2012-05-28 50 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); 
          } 
         }