2012-10-12 46 views
0

我希望能夠從同一個json維護不同的商店,其中每個商店的模型都是相同的。每個商店都需要根據其根屬性分配進行更新。請參閱下面的示例json,商店和模型,在這種情況下,每個商店將根據json的根屬性值(類別1,類別2等)進行更新。目標是能夠將應用程序中的嵌套列表綁定到不同的商店,而不是調用setProxy來更改單個商店中的url設置。另外,json需要採用這種格式。感謝您的幫助,請讓我知道我是否可以提供澄清或回答任何問題。Sencha Touch從同一個Json維護不同的商店

JSON:

{ 
    "items": [ 
     { 
      "name": "category 1", 
      "status": "", 
      "displaytext": "", 
      "items": [ 
       { 
        "name": "", 
        "status": "", 
        "displaytext": "", 
        "items": [ 
         { 
          "name": "", 
          "status": "", 
          "displaytext": "", 
          "items": [ 
           { 
            "name": "", 
            "status": "", 
            "displaytext": "", 
            "leaf": true 
           } 
          ] 
         } 
        ] 
       } 
      ] 
     }, 
     { 
      "name": "category 2", 
      "status": "", 
      "displaytext": "", 
      "items": [ 
       { 
        "name": "", 
        "status": "", 
        "displaytext": "", 
        "items": [ 
         { 
          "name": "", 
          "status": "", 
          "displaytext": "", 
          "leaf": true 
         } 
        ] 
       } 
      ] 
     }, 
     { 
      "name": "cateory 3", 
      "status": "", 
      "displaytext": "", 
      "items": [] 
     }, 
     { 
      "name": "category 4", 
      "status": "", 
      "displaytext": "", 
      "items": [] 
     } 
    ] 
} 

型號:

Ext.define('MyApp.model.myModel', { 
    extend: 'Ext.data.Model', 
    config: { 
     fields: [ 
      { 
       name: 'name', 
       type: 'string' 
      }, 
      { 
       name: 'status', 
       type: 'string' 
      }, 
      { 
       name: 'displaytext', 
       type: 'string' 
      } 
     ] 
    } 
}); 

商店1,2,3等:

Ext.define('MyApp.store.storeCategory1', { 
    extend: 'Ext.data.TreeStore', 
    requires: [ 
     'MyApp.model.myModel' 
    ], 

    config: { 
     model: 'MyApp.model.myModel', 
     storeId: 'myStore', 
     autoLoad: false, 
     proxy: { 
      type: 'ajax', 
      url: '/path/to/file.json', 
      reader: { 
       type: 'json', 
       rootProperty: 'items' 
      } 
     } 
    } 
}); 

回答

0

我想你最好的選擇將是使服務器請求獨立於商店的代理。成功後,根據需要將數據分成不同的商店。以這種方式預處理數據是很好的,特別是如果您需要將一個大型數據響應拆分爲多個數據存儲。例如:

Ext.Ajax.request({ 
    url: 'path/to/file.json', 
    success: function(response){ 
     // process server response here 
     var json = Ext.decode(response.responseText); 
     for(var i=0, l=json.items.length, i<l; i++){ 
      // start distributing the data to your different stores here 
     } 
    } 
}); 

希望這會有所幫助。

相關問題