2013-03-29 70 views
1

我只是試圖讓一個靜態JSON文件加載到TreeStore中,而我正在撕裂我的頭髮。ExtJS 4 TreeStore靜態JSON - 不會加載

我有一個模型:

Ext.define('pronghorn_ui_keyboard.model.CommandKey', { 
    extend: 'Ext.data.Model', 
    fields: [ 
     { 
      name: 'id' 
     }, 
     { 
      name: 'key' 
     }, 
     { 
      name: 'command' 
     } 
    ] 
}); 

我有一個TreeStore:

Ext.define('pronghorn_ui_keyboard.store.Commands', { 
    extend: 'Ext.data.TreeStore', 

    requires: [ 
     'pronghorn_ui_keyboard.model.CommandKey' 
    ], 

    constructor: function(cfg) { 
     var me = this; 
     cfg = cfg || {}; 
     me.callParent([Ext.apply({ 
      storeId: 'commands', 
      model: 'pronghorn_ui_keyboard.model.CommandKey', 
      proxy: { 
       type: 'ajax', 
       url: 'commands.json', 
       reader: { 
        type: 'json' 
       } 
      } 
     }, cfg)]); 
    } 
}); 

而且我有以下的JSON在commands.json:

{ 
    id: 'root', 
    key: null, 
    command: null, 
    children: [ 
     { 
      id: 't' 
      key: 't' 
      command: null, 
      children: [ 
       { 
        id: 'te' 
        key: 'e' 
        command: 'Trade Equity' 
        leaf: true 
       } 
      ] 
     } 
    ] 
} 

我想以編程方式加載此樹並在控制檯中檢查它。在控制器初始化函數:

var me = this; 

me.getCommandsStore().load({ 
    callback: function() { 
     me.rootCommandKey = me.getCommandsStore().getRootNode(); 
     me.currentCommandKey = me.rootCommandKey; 
     console.log(me.currentCommandKey); 
     console.log(me.currentCommandKey.id); 
     console.log(me.currentCommandKey.hasChildNodes()); 
     me.initMainCommands(); 
    }, 
    scope: me 
}); 

控制檯有爲currentCommandKey東西,但ID不是我的根ID,並hasChildNodes()是假的。所以顯然這個文件沒有被加載。

我在做什麼錯?

回答

1

我的JSON無效;基本上缺少逗號。

這裏是正確的JSON:

{ 
    success: true, 
    children: [ 
     { 
      string: 't', 
      key: 't', 
      command: null, 
      children: [ 
       { 
        string: 'te', 
        key: 'e', 
        command: 'Trade Equity', 
        leaf: true 
       } 
      ], 
      leaf: false 
     } 
    ] 
} 

我需要做的錯誤與異步處理的工作做得更好呼叫過於。我將一堆東西移動到Store本身的方法中,並使用本地綁定綁定負載事件的init狀態,從而暴露了一堆負載終止條件標誌。