2012-05-28 74 views
2

我正在創建一個表單,將數據保存到本地存儲中。我正在調用模型中的代理。但我不確定如何從本地存儲獲取數據。本地存儲數據檢索

我的代碼是:

var model = new InfoImage.model.configure.configModel(); 
model.data.servname = servname; 
         model.data.port = port; 
         model.data.protocol = protocol; 
         model.data.username = username; 
         model.data.password = password; 
         model.data.domain = domain; 
         model.data.apptitle = apptitle; 
         model.data.appconfig = appconfig; 
         model.save(); 

         //Ext.getStore('configStore').load(); 
         users = Ext.getStore('configStore').sync(); 
         var item = localStorage.getItem('servname'); 

我的模式是:

//定義的數據結構的工作項列表

Ext.define('InfoImage.model.configure.configModel', { 
    extend : 'Ext.data.Model', 

    config : { 
     //Defining the fields required in the Work Item List 
     fields : [ 'servname', 'port', 'protocol', 'username', 'password', 
       'domain', 'appconfig', 'apptitle', 
       'appconfig' ], 


     proxy : { 
      type : 'localstorage', 
      id : 'configId' 
     } 
    } 
}); 

var item = localStorage.getItem('servname');是給我「空」結果。我有一個商店定義,但我還沒有使用它。任何想法我應該怎麼做呢?

在此先感謝。

+0

哪裏是你店? –

回答

2

如果存儲被稱爲「物品」,如果它的加載,你可以對這些:

var store = Ext.getStore('Items'); // Get the store 
store.add({...}); // Add an instance of you model item 
store.sync(); // Will add the item to the locastorage 
var item = store.getAt(0) // Get the first item in the store 
store.remove(item); // Remove the selected item from the store 
store.sync(); // Will remove the item from the localstorage 

希望這有助於

2

請訪問:http://docs.sencha.com/touch/2-0/#!/api/Ext.data.proxy.LocalStorage

如果你想保存在本地存儲數據,你應該叫yourStore.sync()

如果你想將數據添加到存儲(不localStorage的),你應該叫yourStore.add(object)

然後,如果您需要使用新數據更新本地存儲,則應再次撥打yourStore.sync()

如果您想從本地存儲中填充數據,請致電yourStore.load()

+0

我不想在商店中添加它。我想從本地存儲中檢索值。那可能嗎? – Khush

+1

使用localstorage首次同步存儲數據:users = Ext.getStore('configStore')。sync();然後你可以加載它。如果您沒有將任何數據(使用同步)添加到localstorage中,則無法從中獲取任何數據,因爲它是空的。 –

0
first you define model like below 
Ext.define('LoginDemo.model.User', 
{ 
    extend:'Ext.data.Model', 
    config: 
    { 
     identifier: 'uuid', 
     fields:['name','add','mail'], 
     proxy:`enter code here` 
      { 
       type:'localstorage', 
       id:'mylogin' 
      } 

    } 
}); 
then put model into store using following code 

listeners: 
         { 

          tap:function() 
          { 
           Ext.Msg.alert('Hi'); 


           var store = Ext.create('Ext.data.Store', 
           { 
            model: 'LoginDemo.model.User' 

           }); 

           //loads any existing data from localStorage 
           store.load(); 
           //now add some Searches 
           store.add({name: 'Deepak',add: 'Mumbai',mail:'[email protected]'}, 
              {name: 'Rahul',add: 'Pune',mail:'[email protected]'}); 
           //finally, save our Search data to localStorage 
           store.sync(); 
                   //fetch data locationwise 
           var a=store.getAt(0); 
           console.log(a); 
          } 
         } 
相關問題