2012-02-17 103 views
2

我在與煎茶觸摸和localStorage的幾個問題...煎茶觸摸 - localStorage的問題

1)我能數據保存到本地存儲,直到我刷新頁面,它的偉大工程。在那之後,我仍然可以訪問數據,看起來可以做出更改,甚至可以添加新條目。但只要刷新頁面,我的所有更新都將消失。任何新添加的內容都可以,但是原始條目的更新不會被保存(希望這是有道理的)。基本上,我可以創建一個條目並更新它,直到我刷新頁面。此時它不會再保存到本地存儲。新的條目總是可以很好地保存。 2)當我刪除所有條目,然後刷新頁面時,我在控制檯中得到這個錯誤:「未捕獲的TypeError:無法讀取'null'屬性'id'。它看起來像試圖加載一些東西,即使沒有東西。我不知道爲什麼它會在這種情況下拋出一個錯誤,而不是當頁面最初被加載時,因爲它應該是同樣的事情嗎?

型號:

Ext.regModel('inventoryItem', { 
     idProperty: 'id', 
     fields: [ 
      { name: 'id', type: 'int' }, 
      { name: 'date', type: 'date', dateFormat: 'c' }, 
      { name: 'title', type: 'string' }, 
      { name: 'quantity', type: 'int' }, 
      { name: 'size', type: 'int' }, 
      { name: 'cost', type: 'double' }, 
      { name: 'startDate', type: 'date' }, 
      { name: 'endDate', type: 'date' }, 
      { name: 'dailyAvgCost', type: 'string' }, 
      { name: 'dailyAvgSize', type: 'string' } 
     ] 
    }); 

商店:

Ext.regStore('inventoryItemsStore', { 
     model: 'inventoryItem', 
     sorters: [{ 
      property: 'id', 
      direction: 'DESC' 
     }], 
     proxy: { 
      type: 'localstorage', 
      id: 'inventory-app-store' 
     }, 
     getGroupString: function (record) { 
      return ''; 
     } 
    }); 

處理程序:

 { 
      var currentinventoryItem = CostsApp.views.inventoryEditor.getRecord(); 
      CostsApp.views.inventoryEditor.updateRecord(currentinventoryItem); 
      var inventoryStore = CostsApp.views.inventoryList.getStore(); 
      if (null == inventoryStore.findRecord('id', currentinventoryItem.data.id)) 
      { 
        inventoryStore.add(currentinventoryItem); 
      } 

      inventoryStore.sync(); 
      inventoryStore.sort([{ property: 'date', direction: 'DESC'}]); 
      CostsApp.views.inventoryList.refresh(); 
     } 

任何人都可以指出someth很明顯,我做錯了?

+0

CostsApp.views.inventoryEditor.updateRecord(currentinventoryItem);做?這聽起來像你的模型不知道他們已經出於某種原因被編輯。 – 2012-02-17 06:30:21

回答

0

看起來一切都很好 - store.add()方法與store.sync()一起使用。檢查如何將數據加載到列表中的方式,可能是剛加載的數據。至於我,商店的autoload財產總是不工作(可能是有一些原因,我不否認),而我只是使用store.load()來代替。此外,您可以直接檢查localStorage以確定此確定(即代理ID項localStorage.getItem('inventory-app-store')應該給你類似"1,2,3" - 成功持久記錄的ID)。

這裏是工作的例子,以防萬一。

new Ext.Application({ 
    name: 'MyApp', 
    launch: function() { 
     this.viewport = new Ext.Panel({ 
      fullscreen: true, 
      id : 'mainPanel', 
      layout: 'card', 
      dockedItems: [{ 
       xtype: 'toolbar', 
       dock : 'top', 
       items: [{ 
        xtype: 'textfield', 
        flex: 1 
       }, { 
        text: 'Add', 
        listeners: { 
         tap: function() { 
          var view = this.viewport, 
           list = view.items.first(), 
           store = list.store, 
           fld = view.dockedItems.first().items.first(), 
           val = fld.getValue(); 
          store.add({item: val}); 
          store.sync(); 
          fld.reset(); 
         }, 
         scope: this 
        } 
       }] 
      }], 
      items : [{ 
       xtype: 'list', 
       itemTpl : '{item}', 
       store: new Ext.data.Store({ 
        fields: ['item'], 
        proxy: { 
         id: 'items', 
         type: 'localstorage', 
        } 
       }).load() 
      }] 
     }); 
    } 
}); 
0

如何在if-condition之前調用sync()方法。它爲我工作。

var inventoryStore = CostsApp.views.inventoryList.getStore(); 
inventoryStore.load(); 
inventoryStore.sync(); 
1

存儲同步()對我來說已經證明非常棘手,這可能有助於闡明一些原因。

.sync()將不會更新遠程或本地存儲記錄,除非它們很髒。所以他們不會執着。

使用JSON類型的讀者可以很容易地使直接修改記錄的錯誤,必須使用模型的方法來做到這一點,特別是.set(),因爲集()定義記錄爲「髒」,然後才sync()實際上將保存更改。

這特別難以使用本地存儲進行調試。

對於下列店:

Ext.define('MyApp.store.LocalStuff',{ 
extend: 'Ext.data.Store', 
xtype:'localstuff', 

config: { 
    fields:['stuffId','stuffName','stuffArray'], 
    storeId:'LocalStuffId', 
    autoLoad:false, 
    proxy:{ 
     type:'localstorage', 
     id:'idForLocalStorage', 
     reader: { 
      type: 'json', 
      rootProperty: 'stuffArray' 
     } 
    } 
} 
}); 

假設......你填充店裏的東西,如:

{'stuffArray':[ 
{'stuffId':'130', 'stuffName':'floob', 'stuffArray':[ 
                  'first', 
                  'second', 
                  'tird' //here be a typo (on purpose) 
                  ] 
                 }, 
{'stuffId':'131', 'stuffName':'sateo', 'stuffArray':[ 
                  'primero', 
                  'segundo', 
                  'tercero' 
                  ] 
                 }, 
{'stuffId':'133', 'stuffName':'tolus', 'stuffArray':[ 
                  'prima', 
                  'secunda', 
                  'tertia' 
                  ] 
                 } 
] 
} 

要解決的第一個創紀錄的拼寫錯誤,你可能會嘗試做到以下幾點:

{//in a given controller or view 
someEventHandler:function(){ 
    var stor = Ext.getStore('LocalStuffId'); 
    var firstRecord = stor.getAt(0);//which will get you a record in the form of a Model 
    firstRecord.get('stuffArray')[2]='third';//that will fix it 
    //now we just sync() to make the change persist when we close the app 
    stor.sync(); 
} 
} 

如果您嘗試過,控制檯上將不會出現錯誤,所以,所有我好的。對?

錯!更改將不會在重新啓動時持續。爲什麼?

因爲記錄是「乾淨的」。

我們如何「骯髒」起來?

我發現的唯一方法(我敢肯定有很多更多)是使用方法.SET()從模型更新記錄,那會告訴店裏髒了

這聽起來很奇怪,我的MVC未經訓練的大腦,更好地解決了上面的例子。

{//in a given controller or view 
someEventHandler:function(){ 
    var stor = Ext.getStore('LocalStuffId'); 
    var firstRecord = stor.getAt(0);//which will get you a record in the form of a Model 
    //you might go around the following 2 lines in many ways, the important part is .set() after that 
    var theArrayToFix = firstRecord.get('stuffArray'); 
    theArrayToFix[2]='third'; 

    firstRecord.set('stuffArray',theArrayToFix);//set() dirties the record 
    //NOW we sync() to make the change persist when we close the app 
    stor.sync(); 
} 
} 

所有這一切都可能是有人從煎茶觸摸1來明顯的,但對於像我這樣的新手,它是接近一天的工作要弄清楚。

希望這可以幫助別人,它將在未來可以幫助我。

1

它可能是別的東西。

您正在使用「ID」作爲示範田鍵名,更改到別的東西,如「inventory_id」,然後再試一次。

我的印象是id由localstorage API在內部使用,所以是保留的。

3

我有同樣的問題,我將我的代理移入模型,這解決了我的問題。您的代碼修改應該是這樣的:

型號:

Ext.regModel('inventoryItem', { 
    idProperty: 'id', 
    fields: [ 
     { name: 'id', type: 'int' }, 
     { name: 'date', type: 'date', dateFormat: 'c' }, 
     { name: 'title', type: 'string' }, 
     { name: 'quantity', type: 'int' }, 
     { name: 'size', type: 'int' }, 
     { name: 'cost', type: 'double' }, 
     { name: 'startDate', type: 'date' }, 
     { name: 'endDate', type: 'date' }, 
     { name: 'dailyAvgCost', type: 'string' }, 
     { name: 'dailyAvgSize', type: 'string' } 
    ], 
proxy: new Ext.data.LocalStorageProxy({ 
    id: 'inventory-app-store' 
}) 
}); 

商店:

Ext.regStore('inventoryItemsStore', { 
    model: 'inventoryItem', 
    sorters: [{ 
     property: 'id', 
     direction: 'DESC' 
    }], 
    getGroupString: function (record) { 
     return ''; 
    } 
}); 
1

我知道這老,但也許它會幫助別人。 我有同樣的問題,並嘗試重置我的本地存儲window.localStorage.clear();,它的工作原理。