Sencha Touch (1.1) - “成功”后模型未在本地存储中更新节省

发布于 12-26 23:09 字数 2146 浏览 3 评论 0 原文

我的设置

  • Safari 5.1.2 (Macintosh)
  • Sencha Touch 1.1

问题 过程

有一个模型和一个商店。我使用此处记录的 set(field,value) 方法更新记录:API - 设置。更新字段后,我使用此处记录的 save() 方法:API - 保存

结果

绑定到商店的任何列表中的记录都会更新,并且似乎已保存,但是当我重新加载网页时,旧值显示。因此记录被更新,商店看到新数据,绑定到商店的列表刷新其显示,但本地存储中数据的实际表示永远不会更新。如果您使用浏览器控制台使用 var record = summaryStore.getAt(4) 之类的内容查找和检查项目,您会发现 dirty 属性为 false,即使尚未进行 save() 调用。此外,无论是否调用了 save() 方法,summaryStore.getUpdatedRecords() 都会返回一个空集。

更多文档

API - 模型

API - 存储

API - 代理

代码

存储

var summaryStore = new Ext.data.Store({
model: 'EventSummary',
storeId: 'summaryStore',
sortOnLoad: true,
sorters: [
    {
        property: 'date',
        direction: 'DESC'
    }
],
proxy: {
    type: 'localstorage',
    id  : 'summary-cache'
}
})

模型

Ext.regModel('EventSummary', {
fields: [
    {name: 'child', type: 'string'},
    {name: 'date', type: 'date'},
    {name: 'description', type: 'string'},
    {name: 'event_id', type: 'int'}
],
proxy: summaryStore.proxy
})

示例

var record = summaryStore.findExact('event_id', 90)
record.date = new Date();
record.description = 'Vitamin K Shot`
record.save()

两者 <在 record.save() 之前和之后dirty 标志为 false。 summaryStore.getUpdatedRecords() 在保存之前和之后都会返回一个空集。无论是否保存记录,摘要存储的所有列表都会反映所做的更改。

其他注释 存储

区由此调用填充,该调用位于存储区和模型的声明之后。 summaryStore.load()

My Setup

  • Safari 5.1.2 (Macintosh)
  • Sencha Touch 1.1

The Issue

The process

I have a model and a store. I update a record using the set(field,value) method documented here: API - Set. After updating fields, I use save() method documented here: API - Save.

The Result

The record updates in any lists that are bound to the store and appears to be saved, however when I reload the webpage the old values are displayed. So the record is updated, the store see's the new data, the lists bound to the store refresh their display, but the actual representation of the data in localstorage is never updated. If you use the browser console to find and inspect the item with something like var record = summaryStore.getAt(4), you will find that the dirty attribute is false even if the save() call has not been made. Also summaryStore.getUpdatedRecords() returns an empty set regardless of whether the save() method has been called.

Further documentation

API - Models

API - Stores

API - Proxies

The Code

Store

var summaryStore = new Ext.data.Store({
model: 'EventSummary',
storeId: 'summaryStore',
sortOnLoad: true,
sorters: [
    {
        property: 'date',
        direction: 'DESC'
    }
],
proxy: {
    type: 'localstorage',
    id  : 'summary-cache'
}
})

Model

Ext.regModel('EventSummary', {
fields: [
    {name: 'child', type: 'string'},
    {name: 'date', type: 'date'},
    {name: 'description', type: 'string'},
    {name: 'event_id', type: 'int'}
],
proxy: summaryStore.proxy
})

Example

var record = summaryStore.findExact('event_id', 90)
record.date = new Date();
record.description = 'Vitamin K Shot`
record.save()

Both before and after the record.save(), the dirty flag is false. summaryStore.getUpdatedRecords() returns an empty set both before and after the save. All lists to summary store reflect the change made regardless of whether the record is saved.

Additional Notes

The store is populated with this call, which follows the declaration of the store and the model. summaryStore.load()

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

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

发布评论

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

评论(3

思念绕指尖 2025-01-02 23:09:14

上面提到的 setDirty 解决方法对我不起作用,但我找到了另一种将记录保存到 WebLocalStorage 代理的解决方法:

record.set('field1', 'abc');
store.proxy.setRecord(record);

我正在使用 1.1.1。
(顺便说一句,由于某种原因,我无法调用 save();例外情况是我需要一个 url,这不适用于 weblocalstorage。)

The setDirty workaround mentioned above didn't work for me, but I found another workaround for saving records to a WebLocalStorage proxy:

record.set('field1', 'abc');
store.proxy.setRecord(record);

I'm using 1.1.1.
(And btw for some reason I can't call save(); the exception says I need a url, which is inapplicable to weblocalstorage.)

痴情 2025-01-02 23:09:14

使用存储上的sync()方法将数据保存到本地存储。

尝试使用此代码而不是上面的示例:

var record = summaryStore.findExact('event_id', 90)
record.set('date', new Date());
record.set('description', 'Vitamin K Shot`);
record.save();

summaryStore.sync();

Use the sync() method on the store to persist the data to localstorage.

Try this code instead of your example above:

var record = summaryStore.findExact('event_id', 90)
record.set('date', new Date());
record.set('description', 'Vitamin K Shot`);
record.save();

summaryStore.sync();
过期以后 2025-01-02 23:09:14

更新

通过在将模型实例传递给商店的 add 方法之前正确实例化模型实例,可以完全避免此问题。我只是传递 JSON,商店接受了它,但显然引起了问题。

结束更新

该问题可以通过在 WebStorageProxy.js 中进行更改来解决。

请注意,更改此文件不会修改您的 sencha-touch.js 文件。您必须重建它或直接修改它

此代码不起作用

setRecord: function(record, id) {
    if (id) {
        record.setId(id);
    } else {
        id = record.getId();
    }

此行特别是id = record.getId();由于某种原因在已经存在的对象上未定义已添加到商店。解决方案是添加

if (id === undefined) {
    id = record.internalId;
}

最终代码如下

setRecord: function(record, id) {
    if (id) {
        record.setId(id);
    } else {
        id = record.getId();
        if (id === undefined) {
            id = record.internalId;
        }
    }

Update

The issue can be avoided altogether by properly instantiating your model instances before passing them to the store's add method. I was just passing in JSON, which the store accepted, but apparently was causing issues.

End Update

The issue can be fixed by making changes in WebStorageProxy.js

Be advised that changing this file does not modify your sencha-touch.js file. You will have to rebuild it or modify it directly

This code was not working

setRecord: function(record, id) {
    if (id) {
        record.setId(id);
    } else {
        id = record.getId();
    }

This line in particular id = record.getId(); for some reason was coming up undefined on objects that had already been added to the store. The solution is to add

if (id === undefined) {
    id = record.internalId;
}

The final code looks like

setRecord: function(record, id) {
    if (id) {
        record.setId(id);
    } else {
        id = record.getId();
        if (id === undefined) {
            id = record.internalId;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文