2012-03-29 88 views
0

當我運行這個示例時,它會導致一個錯誤:「Uncaught TypeError:無法調用未定義的方法get',問題是sotre1在呈現列時長度爲0,但我找不到解決方案來解決這個。場景可能不合理,但我只想使用獨立商店呈現網格列,有沒有一種方法可以確保store1在呈現列之前加載? 這裏是代碼:可能Ext JS使用商店沒有附加到網格呈現網格列?

Ext.onReady(function(){ 
Ext.define('File', { 
    extend: 'Ext.data.Model', 
    fields: ['id', 'name', 'pid'] 
}); 
var store = Ext.create('Ext.data.Store', { 
    model: 'File', 
    //fields: ['id', 'name', 'pid'], 
    proxy: { 
     type: 'ajax', 
     url: 'file.json', 
     reader: { 
      type: 'json', 
      root: 'files', 
      successProperty: 'success' 
     } 
    }, 
    autoLoad: true 
}); 
var store1 = Ext.create('Ext.data.Store', { 
    model: 'File', 
    proxy: { 
     type: 'ajax', 
     url: 'file.json', 
     reader: { 
      type: 'json', 
      root: 'files', 
      successProperty: 'success' 
     } 
    }, 
    autoLoad: true 
}); 
Ext.create('Ext.grid.Panel', { 
    renderTo: Ext.getBody(), 
    title: 'File info', 
    store: store, 
    columns: [ 
     { 
      header: 'ID', 
      dataIndex: 'id' 
     }, 
     { 
      header: 'Name', 
      dataIndex: 'name' 
     }, 
     { 
      header: 'ParentName', 
      dataIndex: 'pid', 
      renderer: function(pid){ 
       var index = store1.find('id', pid); // find it's parent 
       console.log(index); 
       var name = store1.getAt(index).get('name'); 
       return name; 
      } 
     } 
    ] 
}); 

}); 

這裏是JSON文件:

{ 
success: true, 
files: [ 
    {id: 1, name: 'root', pid: 1}, 
    {id: 2, name: 'sub1', pid: 1}, 
    {id: 3, name: 'sub2', pid: 1}, 
    {id: 4, name: 'sub1-1', pid: 2}, 
    {id: 5, name: 'sub1-2', pid: 2} 
] 
} 

回答

0

可能更容易壓扁在服務器端的結構和各子記錄一起發送父名稱。

我發現另一件事你的商店是相同的,爲什麼不只是使用1?

+0

這實際上是一樣的,但我只是想用它沒有連接到電網的店呈現網格的列(本演示中的store1) – neil 2012-03-29 12:40:55

+0

爲什麼你不能使用同一個商店? – dbrin 2012-03-29 18:14:10

0

這可能是一個與AJAX調用的異步性質相關的問題,因此在訪問其記錄時無法加載store1。

你可以試着放置Ext.create store1.load的回調()內撥打:

store1.load({ 
    callback: function() { 
     Ext.create('Ext.grid.Panel', ...); 
    } 
});