2014-09-23 80 views
0

我試圖將一些應用程序設置保存到LocalStorage。 我有一個與LocalStorageProxy模型,我有一個商店與模型和autoload = true。 我在添加記錄後同步商店。 相應的文檔:http://docs-origin.sencha.com/touch/2.4.0/#!/api/Ext.data.proxy.LocalStorage 但是,在瀏覽器(Chrome)中重新加載應用程序後,數據將丟失。添加到自動加載標誌,我在應用程序的啓動方法中執行手動store.load()。 所有工作正常,直到我重新加載應用程序。 有關於此的任何想法?謝謝!Sencha Touch 2 LocalStorageProxy不會將數據保存到LocalStorage

模型:

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(); 
    } 
}, 

回答

0

我想我已經找到了解決方案。 似乎LocalstorageProxy在給定對象標識符方面存在問題。 默認情況下,我將對象ID設置爲0,因爲我一次只需要一個對象,並將所有設置放在一個對象中。 不幸的是,本地存儲無法將其寫入瀏覽器存儲。 不,我已經重寫了代碼,以便sencha可以自己設置Id。現在它似乎工作正常。 而且我可以通過從商店中調用.first()函數來訪問記錄。

新的代碼片段:

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(); 

我希望這將幫助別人的未來。