2011-01-25 67 views
1

我试图在Sencha Touch应用程序中使用localStorage中的元素构建商店。在Sencha Touch中使用localStorage存储

的localStorage的我想要得到的数据是从的localStorage [“饲料”],看起来像这样:

"[{"title":"Title 1","url":"http://stackoverflow.com"}, 
    {"title":"Title2","url":"http://google.com"}]" 

我想有以下让它进店:

var feedsPanel; 
var store; 
Ext.setup({ 
icon: 'icon.png', 
glossOnIcon: false, 
onReady: function(){ 
    Ext.regModel("feedModel", { 
     fields: [ 
      { name: "title", type: "string" }, 
      {name: "url", type:"string"} 
     ] 
    }); 
    store = new Ext.data.Store({ 
      proxy: new Ext.data.LocalStorageProxy({ 
       id: 'feeds' 
      }), 
      model:"feedModel" 
    }); 

当我在Chrome中尝试store.load()时,由于TypeError而失败:无法读取null属性的'标题'。

我该如何访问这个localStorage的每个标题和每个url?

看看纸牌游戏的例子只是让我头晕。

我的Sencha应用程序的其余部分不依赖于此存储,并且完全加载。我使用Chrome浏览器检查商店中是否有商品。

回答

1

这种localStorage格式并非特定于Sencha的商店。但是,如果您真的需要以这种方式格式化localStorage,则可以尝试以下操作。这是可能的:-)

// first prefill localStorage 
var feeds = [], j = 0; 
while (j < 25) { 
    feeds.push({'title': 'Title ' + ++j, url: 'http://link' + j + '.com'}); 
} 
localStorage.setItem('feeds', Ext.encode(feeds)); 

// Sencha Touch v1 Application 
new Ext.Application({ 
    name: 'App', 

    launch: function() { 
     var store = new Ext.data.JsonStore({ 
      id: 'feedstore', 
      fields: ['title', 'url'], 
      autoload: true, 
      proxy: { 
       id: 'feedproxy', 
       type: 'memory', 
       url: 'feeds', 
       create: function() { 
        console.log('feed: CREATE'); 
       }, 
       read: function (operation, callback, scope) { 
        console.log('feed: READ'); 

        var data = localStorage.getItem(this.url), 
         i, len, records = []; 

        console.log('data: ' + data); 
        data = Ext.decode(data); 

        if (Ext.isArray(data)) { 
         len = data.length; 
         for (i = 0; i < len; ++i) { 
          records.push(new this.model(data[i])); 
         }; 

         // return model instances in a result set 
         operation.resultSet = new Ext.data.ResultSet({ 
          records: records, 
          total : len, 
          loaded : true 
         }); 

         // announce success 
         operation.setSuccessful(); 
         operation.setCompleted(); 

         // finish with callback 
         if (typeof callback == "function") { 
          callback.call(scope || this, operation); 
         } 
         Ext.getBody().unmask(); 
        } 
       }, 
       update: function() { 
        console.log('feed: UPDATE'); 
       }, 
       destroy: function() { 
        console.log('feed: DESTROY'); 
       }, 
       reader: { 
        type: 'json' 
       } 
      } 
     }).load(); 

     this.viewport = new Ext.Panel({ 
      fullscreen: true, 
      layout: 'card', 
      items: [{ 
       xtype: 'list', 
       itemTpl : '{title}<br />{url}', 
       store: store 
      }] 
     }); 
    } 
}); 
1

本地存储器中是否已有其中具有不同模型“模式”的条目?只是一个想法:尝试一个不同的代理ID。

但我认为你最好注册一个商店,而不是直接实例化它。尝试:

Ext.regStore('store', { 
    proxy: {...} 
    ... 
); 

然后

store:'store' 
在列表或任何其结合到它

+0

我只是不明白它使用的方案。我以为我可以访问列表中的所有关联数组,这些数组在与Sencha Touch无关的脚本中被串化为localStorage,但似乎不可能。 – andersem 2011-01-31 23:24:11