【问题标题】:Sencha Touch 2 LocalStorageProxy dosn't save data to LocalStorageSencha Touch 2 LocalStorageProxy 不将数据保存到 LocalStorage
【发布时间】:2014-09-23 10:29:14
【问题描述】:

我正在尝试将一些应用设置保存到 LocalStorage。 我有一个带有 LocalStorageProxy 的模型,我有一个带有模型和 autoload = true 的商店。 添加记录后我同步商店。 对应文档:http://docs-origin.sencha.com/touch/2.4.0/#!/api/Ext.data.proxy.LocalStorage 但是在浏览器(Chrome)中重新加载应用程序后,数据会丢失。除了自动加载标志之外,我还在应用程序的启动方法中执行了手动 store.load()。 一切正常,直到我重新加载应用程序。 有什么想法吗?谢谢!

型号:

Ext.define('LwvMediaReminder.model.Setting', {

    extend: 'Ext.data.Model',

    requires: [
        'Ext.data.Field',
        'Ext.data.proxy.LocalStorage'
    ],


    config: {
    idProperty: 'settingsId',
    fields: [
        {
            name: 'settingsId',
            type: 'int'
        },
        {
            name: 'bPush',
            type: 'boolean'
        },
        {
            name: 'bEmail',
            type: 'boolean'
        },
        {
            name: 'sEmail',
            type: 'string'
        },
        {
            name: 'sToken',
            type: 'string'
        }
    ],
    proxy: {
        type: 'localstorage',
        id: 'settings'
    }
}
});

商店

Ext.define('LwvMediaReminder.store.SettingsStore', {

extend: 'Ext.data.Store',
alias: 'store.SettingsStore',


requires: [
    'LwvMediaReminder.model.Setting'
],


config: {
    autoLoad: true,
    model: 'LwvMediaReminder.model.Setting',
    storeId: 'SettingsStore'
}
});

我的 SettingsController 中应该保存设置对象的方法,我总是只使用一条 id 为 0 的记录:

onSaveSettingsButtonTap: function(button, e, eOpts) {        var mainView = this.getMainView(),
        store = Ext.getStore('SettingsStore'),
        properties = {
            settingsId: 0,
            bPush:  this.getPushCheckbox().isChecked(),
            bEmail: this.getEmailCheckbox().isChecked(),
            sEmail: this.getEmailField().getValue(),
            sToken: this.getTokenField().getValue()
        },
        record = store.getById(0),
        save = true;

    // some form validation
    if (properties.bEmail) {
        if (properties.sEmail === '') {
            save = false;
            Ext.Msg.alert('Fehler', 'Bitte geben sie auch eine gültige Email-Adresse an.');
        }
        if (!this.validateEmail(properties.sEmail)) {
            save = false;
            Ext.Msg.alert('Fehler', 'Die angegebene Email-Adresse ist nicht gültig.');
        }
    }

    // save the record
    if (save) {
        if (null !== record) {
            record.set(properties);
        } else {
            store.add(properties);
        }
        store.sync();
        mainView.pop();
    }
},

【问题讨论】:

    标签: javascript html extjs touch local-storage


    【解决方案1】:

    我想我已经找到了解决方案。 似乎 LocalstorageProxy 存在给定对象 ID 的问题。 默认情况下,我将对象 ID 设置为 0,因为我一次只想要一个对象,而我的所有设置都在一个对象中。 不幸的是,本地存储无法将其写入浏览器存储。 不,我已经重写了代码,以便 sencha 可以自行设置 Id。现在它似乎工作正常。 我可以通过调用 store 中的 .first() 函数来访问记录。

    新代码sn-p:

    var store = Ext.getStore('SettingsStore'),
    // no preset id anymore
    properties = {
        bPush:  this.getPushCheckbox().isChecked(),
        bEmail: this.getEmailCheckbox().isChecked(),
        sEmail: this.getEmailField().getValue(),
        sToken: this.getTokenField().getValue()
    },
    // reading the first entry because there is always only one in my app
    record = store.first();
    

    我希望这对将来的人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多