2011-05-28 24 views
2

喂那裏工作,獲取LocalStorageProxy與TreeStore

我建立一個煎茶觸摸的iPhone應用程序,它利用TreeStore其與NestedList很好的工作,但我已經打了一堵牆。

它需要一種方法來讀取靜態存儲(TreeStore)中的數據,將其加載到localStorage中,以便它可寫,然後保存一些用戶首選項。我發現localStorageProxy最有可能是我需要的(我發現我沒有必要手動對JSON存儲進行序列化和反序列化),但是在閱讀了它的文檔並嘗試並失敗了一些東西之後,我發現不知所措。

就目前而言,它會生成localStorage ...存儲但它是空的,我猜這是因爲我沒有加載任何東西,但在做app.stores.event_store.load();屏幕是空白的,Console顯示localStorage爲空且沒有錯誤。

我的模型:

 
Ext.regModel('Events', { 
    fields: [ 
     {name: 'text',  type: 'string'}, 
     {name: 'info',  type: 'string'}, 
     {name: 'attend', type: 'boolean', defaultValue: 'false'} 
    ] 
}); 

我的店:

 
app.stores.event_store = new Ext.data.TreeStore({ 
    model: 'Events', 
    root: data, 
    storeId: 'eventStoreId', 
    proxy: { 
     type: 'localstorage', 
     id: 'eventsAttending', 
     url: 'store.js', 
     reader: { 
      type: 'tree', 
      root: 'items' 
     } 
    } 
}); 

app.stores.event_store.load(); 

它可能是從一個缺乏如何將這些模型與存儲交互的基本認識莖,但如果有人可以幫助,好人緣會發送你的方式。

完整的應用程序可以在it's GitHub repo找到。

-fetimo

回答

3

的TreeStore預計分層數據

var data= { 
    text: 'Groceries', 
    items: [{ 
     text: 'Drinks', 
     items: [{ 
      text: 'Water', 
      items: [{ 
       text: 'Sparkling', 
       leaf: true 
      },{ 
       text: 'Still', 
       leaf: true 
      }] 
     },{ 
      text: 'Coffee', 
      leaf: true 
     }] 
    }] 
} 

但LocalStorageProxy無法爲這個數據結構模型相匹配。

我會使用AJAX請求來使用localStorage直接加載「store.js」文件來「存儲」數據blob。

Ext.Ajax.request({ 
    url: 'store.js', 
    success: function(response, opts) { 
     localStorage.StoreJSBlob = response.responseText; 
    } 
}); 

那麼你的店將是這個樣子:

var store = new Ext.data.TreeStore({ 
    model: 'Events', 
    root: localStore.StoreJSBlob, 
    proxy: { 
     type: 'ajax', 
     reader: { 
      type: 'tree', 
      root: 'items' 
     } 
    } 
}); 

你只需要鏈中的事件一起正確,因爲異步調用都可發覺你

+0

你有工作示例爲了這? – 2011-08-18 02:40:19

+0

@亞當,你可以有一個工作樣本http://SenchaFiddle.com – Neutralizer 2011-12-28 06:29:40

+0

我擔心這是唯一的出路。多可惜。我花了很多時間讓localStorage商店爲我的基本清單工作! – Primus202 2013-09-12 19:55:27