2011-01-25 3 views
1

Sencha Touch 응용 프로그램에서 localStorage의 요소를 사용하여 상점을 구축하려고합니다.Sencha Touch의 매장에서 localStorage 사용

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" 
    }); 
: 나는 다음과 같이 가게에 그것을 얻으려고

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

을 : 로컬 스토리지 [ "피드"]과 같이 보입니다에서

로컬 스토리지는 내가 데이터를 얻을 싶어

Chrome에서 store.load()를 시도하면 TypeError 때문에 실패합니다. null의 'title'속성을 읽을 수 없습니다.

이 localStorage의 모든 제목과 모든 URL에 어떻게 액세스해야합니까?

솔리테어 게임의 예를 보면 나를 어지럽게합니다.

나머지 Sencha 응용 프로그램은 현재이 저장소에 의존하지 않으며 완벽하게로드됩니다. Chrome에 콘솔이있는 상점의 항목이 있는지 확인합니다.

답변

1

이러한 localStorage 형식은 Sencha 's Stores에만 해당되지 않습니다. 그러나 그런 식으로 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