Sencha Touch 同步不保存到代理
Sencha Touch synch not saving to proxy
我在 Sencha Touch v2 控制器中有一段代码,如下所示。当此代码为 运行 时,store.add 之后的计数为 (6),store.sync 之后的计数器为 (0),并且 store.sync 成功。
注意:items 包含从 JsonP 代理接收的 6 条记录,控制台中没有显示任何错误,一切都像完全成功一样。
requires: [
'Ext.data.proxy.JsonP',
'Ext.data.proxy.Sql'
],
config: {
models: ['Convention'],
stores: ['Conventions']
},
// ***additional code*** //
store.setProxy({
type: 'sql',
database: 'app',
table: 'Conventions'
});
store.removeAll();
store.sync({
success: function(batch){
store.add(items);
console.log("Count before: " + store.getCount()); << Shows (6)
store.sync({
failure: function (batch, options) {
console.log('Convention Sql failure');
},
success: function(batch,options){
console.log("Count after: " + store.getCount()); << Shows (0)
console.log("Convention Sql success");
store.load();
Ext.Viewport.unmask();
}
});
}
});
模型在这里显示
extend: 'Ext.data.Model',
config: {
idProperty: 'id',
fields: [
{ name: 'id', type: 'string' },
{ name: 'name', type: 'string' },
{ name: 'logoId', type: 'string' },
{ name: 'seasonId', type: 'string'},
{ name: 'viewed', type: 'string'},
{ name: 'dateCreated', type: 'string'},
{ name: 'dateUpdated', type: 'string'}
]
}
商店就在这里
extend: 'Ext.data.Store',
requires: [
'Ext.data.proxy.Sql'
],
config: {
model: 'app.model.Convention',
autoLoad: false,
sorters:[{
property: 'name',
direction: 'ASC'
}],
proxy: {
type: 'sql',
database: 'app',
table: 'Conventions'
}
}
经过大量研究,似乎 Sencha 最近更改了一些关于如何处理 ID 的代码。因此,如果您使用的是此更改之前的代码示例,您将在尝试保存到 ID 字段时遇到错误。
Sencha 添加了 clientIdProperty,您可以将其添加到代理的编写器并直接添加到模型。
参考:http://docs.sencha.com/touch/2.4/2.4.2-apidocs/#!/api/Ext.data.Model-cfg-clientIdProperty
The name of a property that is used for submitting this Model's unique client-side identifier to the server when multiple phantom records are saved as part of the same Operation. In such a case, the server response should include the client id for each record so that the server response data can be used to update the client-side records if necessary. This property cannot have the same name as any of this Model's fields.
因此,我上面的代码将对以下代码进行以下更改,其中 'siteId' 是从 Json 请求传递的我的 ID 以及我服务器上用于记录的 ID。
型号
idProperty: 'id',
clientIdProperty: 'siteId',
identifier: 'uuid',
控制器
store.setProxy({
type: 'sql',
database: 'app',
table: 'Conventions',
writer: {
clientIdProperty: 'siteId'
}
});
我在 Sencha Touch v2 控制器中有一段代码,如下所示。当此代码为 运行 时,store.add 之后的计数为 (6),store.sync 之后的计数器为 (0),并且 store.sync 成功。
注意:items 包含从 JsonP 代理接收的 6 条记录,控制台中没有显示任何错误,一切都像完全成功一样。
requires: [
'Ext.data.proxy.JsonP',
'Ext.data.proxy.Sql'
],
config: {
models: ['Convention'],
stores: ['Conventions']
},
// ***additional code*** //
store.setProxy({
type: 'sql',
database: 'app',
table: 'Conventions'
});
store.removeAll();
store.sync({
success: function(batch){
store.add(items);
console.log("Count before: " + store.getCount()); << Shows (6)
store.sync({
failure: function (batch, options) {
console.log('Convention Sql failure');
},
success: function(batch,options){
console.log("Count after: " + store.getCount()); << Shows (0)
console.log("Convention Sql success");
store.load();
Ext.Viewport.unmask();
}
});
}
});
模型在这里显示
extend: 'Ext.data.Model',
config: {
idProperty: 'id',
fields: [
{ name: 'id', type: 'string' },
{ name: 'name', type: 'string' },
{ name: 'logoId', type: 'string' },
{ name: 'seasonId', type: 'string'},
{ name: 'viewed', type: 'string'},
{ name: 'dateCreated', type: 'string'},
{ name: 'dateUpdated', type: 'string'}
]
}
商店就在这里
extend: 'Ext.data.Store',
requires: [
'Ext.data.proxy.Sql'
],
config: {
model: 'app.model.Convention',
autoLoad: false,
sorters:[{
property: 'name',
direction: 'ASC'
}],
proxy: {
type: 'sql',
database: 'app',
table: 'Conventions'
}
}
经过大量研究,似乎 Sencha 最近更改了一些关于如何处理 ID 的代码。因此,如果您使用的是此更改之前的代码示例,您将在尝试保存到 ID 字段时遇到错误。
Sencha 添加了 clientIdProperty,您可以将其添加到代理的编写器并直接添加到模型。
参考:http://docs.sencha.com/touch/2.4/2.4.2-apidocs/#!/api/Ext.data.Model-cfg-clientIdProperty
The name of a property that is used for submitting this Model's unique client-side identifier to the server when multiple phantom records are saved as part of the same Operation. In such a case, the server response should include the client id for each record so that the server response data can be used to update the client-side records if necessary. This property cannot have the same name as any of this Model's fields.
因此,我上面的代码将对以下代码进行以下更改,其中 'siteId' 是从 Json 请求传递的我的 ID 以及我服务器上用于记录的 ID。
型号
idProperty: 'id',
clientIdProperty: 'siteId',
identifier: 'uuid',
控制器
store.setProxy({
type: 'sql',
database: 'app',
table: 'Conventions',
writer: {
clientIdProperty: 'siteId'
}
});