【问题标题】:Sencha Touch synch not saving to proxySencha Touch 同步不保存到代理
【发布时间】:2015-12-30 04:30:18
【问题描述】:

我在 Sencha Touch v2 控制器中有一段代码,如下所示。运行此代码时,store.add 后的计数为 (6),store.sync 后的计数器为 (0),store.sync 成功。

注意:项目包含从 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'
    }
}

【问题讨论】:

  • 深入挖掘,如果我从项目中删除 ID 并让模型执行自动 ID,它就可以工作。我只能假设我需要定义我的 ID。有什么建议吗?

标签: sencha-touch-2


【解决方案1】:

经过大量研究,Sencha 最近似乎更改了一些关于如何处理 ID 的代码。因此,如果您使用此更改之前的代码示例,则在尝试保存到 ID 字段时会出错。

Sencha 添加了 clientIdProperty,您可以将其添加到代理的编写器并直接添加到模型中。

参考:http://docs.sencha.com/touch/2.4/2.4.2-apidocs/#!/api/Ext.data.Model-cfg-clientIdProperty

当多个幻像记录保存为同一操作的一部分时,用于将此模型的唯一客户端标识符提交给服务器的属性名称。在这种情况下,服务器响应应该包含每个记录的客户端 ID,以便在必要时可以使用服务器响应数据来更新客户端记录。此属性不能与此模型的任何字段同名。

因此,我上面的代码将对以下内容进行以下更改,其中“siteId”是从 Json 请求传递的我的 id,以及我的服务器上用于记录的 ID。

型号

    idProperty: 'id',
    clientIdProperty: 'siteId',
    identifier: 'uuid',

控制器

    store.setProxy({
        type: 'sql',
        database: 'app',
        table: 'Conventions',
        writer: {
            clientIdProperty: 'siteId'
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 2012-08-02
    相关资源
    最近更新 更多