0

使用sencha touch,我想將我的數組列表數據存儲在localStorage中,如圖所示。如下使用sencha touch在localStorage中存儲數組數據對象

enter image description here

我簡單的存儲數據,如日期,編號,名稱中的localStorage在圖書城刷新功能顯示在圖像

enter image description here

onStore刷新功能。但是找不到將arrayObject直接存儲到localStorage的方式。

回答

1

我不知道我完全理解你的問題..

如果你想店本地商店中的JavaScript對象,可以通過使用將javascript對象轉換爲字符串來實現方法。

var testString = JSON.stringify(testObj); //Converts testObject to Json string. 
localStorage.testString = testString; // Stores testString into local storage. 

您可以從本地存儲獲得的TestString,它通過

var testObj = JSON.parse(localStorage.testString); 

轉換爲JavaScript對象或者你可以使用

Ext.encode(); // Encodes an Object, Array to String. 

Ext.decode(); // Decodes (parses) a JSON string to an object. 
0

你可以存儲完整的對象,但如果你只是想存儲主題,那麼你需要創建新的模型。 定義了一個存儲主題的模型,或者您可以存儲您獲取的完整對象。

Ext.define('Demo.model.Subjects', { 
    extend: 'Ext.data.Model', 
    config: { 
     fields: [ 
      { name: 'id', type: 'int' }, 
      { name: 'Subjects', type: 'auto' } 
     ] 
    } 
}); 

定義本地存儲

Ext.define('Demo.store.MySubjectLocalStore', { 
    extend: 'Ext.data.Store', 
    config: { 
     storeId: 'mySubjectLocalStore', 
     model: 'Demo.model.Subjects', 
     autoLoad: true, 
     clearOnPageLoad: true, 
     proxy: { 
      type: 'localstorage', 
      id: 'demo-mySubjects-local-store' 
     } 
    } 
}); 

將數據存儲在本地存儲

var subjectsLocal = Ext.getStore('mySubjectLocalStore'); 

//loop to get your subjects array from main Object then add each array to localstore 
//loop start 
var subjects = //get it from parent object inside loop 
var subjectsModel = Ext.create('Demo.model.Subjects', { 
    Subjects : subjects, 
    id: //use integer so that you can get by this id when you retrive it 
}); 

subjectsLocal.add(subjectsModel); 
//loop End 

subjectsLocal.sync(); 
+0

其實我創造我的商店和模型作爲參考從這個例子。 http://stackoverflow.com/questions/14797465/sencha-store-and-model-set-up-for-use-with-json 所以我有2個ListViews。其中一個視圖與BookStore和第二個ListView與ProductList Store連接。 我的Ajax請求將由BookStore調用,並在其上載入我將所有數據設置爲Product Store。 現在我的bookStore數據存儲在localstorage中,但productList Data(SubjectList)無法保存。 –

相關問題