Sencha Touch 2 Beta 2 商店同步问题

发布于 2025-01-04 01:19:06 字数 355 浏览 2 评论 0原文

在 ST1.x 中,我使用以下方法将在线商店同步到离线商店没有问题,现在看来同步在 STB2 中不起作用。我可以看到控制台上输出的记录。还有其他人遇到这个问题吗?我相信这可能是一个错误......

var remoteStore = Ext.getStore('UpdateConfig');
var localStore = Ext.getStore('UpdateLocalConfig');

remoteStore.each(function (record) {
    localStore.add(record.data);
    console.log(record.data);
});
localStore.sync();

In ST1.x I had no problem syncing an onlinestore to an offlinestore with the below method, now it seems that sync doesn't work in STB2. I can see the records being output on the console. Anyone else having this issue? I believe it may be a bug...

var remoteStore = Ext.getStore('UpdateConfig');
var localStore = Ext.getStore('UpdateLocalConfig');

remoteStore.each(function (record) {
    localStore.add(record.data);
    console.log(record.data);
});
localStore.sync();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

<逆流佳人身旁 2025-01-11 01:19:06

同样的问题+答案@ Sencha 论坛

.. .和相同的用户??? XD

same question + answer @ Sencha Forum

...and same user??? XD

好多鱼好多余 2025-01-11 01:19:06

Sencha Touch 2 论坛对此问题进行了解答作者:TommyMaintz,但我也想在这里给出答案。

“我认为我发现错误的一件事是,您使用 record.data 将记录添加到 LocalStore。在 ST2 中,我们现在有一个模型缓存。这意味着,如果您创建两个具有完全相同模型和 id 的实例,第二次创建该实例时,它只会返回已经存在的实例,这意味着如果您同步本地存储,它不会将该记录识别为“虚拟”记录,因为它已经具有您想要的 id。如果你想的话,必须在你的情况下做通过使用所有数据但删除 id 来生成记录的“副本”,这将为它生成一个新的简单 id,当您将其保存到本地存储时,它将为其生成一个正确的本地 id

。我注意到模型上的“复制”方法尚未更新来处理此问题,如果您应用以下覆盖,您应该能够执行 localStore.add(record.copy());

Ext.define('Ext.data.ModelCopyFix', {
    override: 'Ext.data.Model',

    /**
     * Creates a copy (clone) of this Model instance.
     *
     * @param {String} id A new id. If you don't specify this a new id will be generated for you.
     * To generate a phantom instance with a new id use:
     *
     *     var rec = record.copy(); // clone the record with a new id
     *
     * @return {Ext.data.Model}
     */
    copy: function(newId) {
        var me = this,
            idProperty = me.getIdProperty(),
            raw = Ext.apply({}, me.raw),
            data = Ext.apply({}, me.data);

        delete raw[idProperty];
        delete data[idProperty];

        return new me.self(null, newId, raw, data);
    }
});

This was answered on the Sencha Touch 2 Forums by TommyMaintz, but I wanted to give the answer here as well.

"One thing I think I see which is wrong is that you are adding a record to the LocalStore using the record.data. In ST2 we now have a Model cache. This means that if you create two instances with the exact same model and id, the second time you create that instance it will just return the already existing instance. This means that if you sync your local store, it won't recognize that record as a 'phantom' record because it already has an id. What you would have to do in your case if you want to make a "copy" of your record by using all the data but removing the id. This will generate a new simple id for it and when you save it to your local storage it will generate a proper local id for it.

When I tried doing this I noticed the "copy" method on Model hasn't been updated to handle this. If you apply the following override you should be able to do localStore.add(record.copy()); localStore.sync()"

Ext.define('Ext.data.ModelCopyFix', {
    override: 'Ext.data.Model',

    /**
     * Creates a copy (clone) of this Model instance.
     *
     * @param {String} id A new id. If you don't specify this a new id will be generated for you.
     * To generate a phantom instance with a new id use:
     *
     *     var rec = record.copy(); // clone the record with a new id
     *
     * @return {Ext.data.Model}
     */
    copy: function(newId) {
        var me = this,
            idProperty = me.getIdProperty(),
            raw = Ext.apply({}, me.raw),
            data = Ext.apply({}, me.data);

        delete raw[idProperty];
        delete data[idProperty];

        return new me.self(null, newId, raw, data);
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文