2011-08-04 42 views
0

我的問題包括無法通過關聯檢索數據。 從控制檯運行setup()後,我希望firstTurbine.getPlant()返回關聯的工廠,但它返回undefined。 我花了很多時間尋找解決方案,我可能不是正確的地方。數據模型:Association getter返回undefined

相關碼被附加如下:

Ext.regApplication({ 
      name: "app", 
      launch: function() { 
       //app.views.viewport = new app.views.Viewport(); 
      } 
}); 

app.models.Plant = Ext.regModel("Plant", { 
    fields: [ 
     {name: "id", type: "int"}, 
     {name: "name", type: "string"}, 
     {name: "notes", type: "auto"} 
    ], 
    proxy: {type: 'localstorage', id:'plantStorage'} 
}); 

app.models.Turbine = Ext.regModel("Turbine", { 
    fields: [ 
      {name: "id", type: "int"}, 
      {name: "plant_id", type: "int"}, 

      {name: "name", type: "string"}, 
      {name: "notes", type: "auto"} 
      ], 
    proxy: {type: 'localstorage', id:'turbineStorage'}, 
    belongsTo: 'Plant' 
}); 

app.stores.plants = new Ext.data.Store({ 
    model: "Plant", 
    autoLoad: true, 
    data : [ 
      {id: 1, name: 'Plant1', notes: ["Note1", "Note2"]}, 
      {id: 2, name: 'Plant2', notes: ["Note1", "Note2"]}, 
      {id: 3, name: 'Plant3', notes: ["Note1", "Note2"]} 
     ] 
}); 

app.stores.turbines = new Ext.data.Store({ 
    model: "Turbine", 
    autoLoad: true, 
    data: [ 
      {id: 11, "plant_id": 1, name: "T41", notes: ["Turbine note 1", "Turbine note 2"]}, 
      {id: 12, "plant_id": 1, name: "T13", notes: ["Turbine note 1", "Turbine note 2"]} 
      ] 
}); 

function setup(){ 
    firstPlant = app.stores.plants.getAt(0); 
    if(!firstPlant){ 
     firstPlant = Ext.ModelMgr.create({name:"TestPlant1", id: 1}, "Plant"); 
     app.stores.plants.add(firstPlant); 
     app.stores.plants.sync(); 
    } 

    firstTurbine = app.stores.turbines.getAt(0); 
    if(!firstTurbine){ 
     firstTurbine = Ext.ModelMgr.create({name:"T31", id: 30, plant_id: 1}, "Turbine"); 
     app.stores.turbines.add(firstTurbine); 
     app.stores.turbines.sync(); 
    } 

    return {firstTurbine: firstTurbine, firstPlant: firstPlant}; 
} 
+0

閱讀我的[關於模型+協會+專賣店答案](http://stackoverflow.com/questions/6622878/ ExtJS的-4-機型與 - 協會和 - 存儲/ 6888446#6888446)。我認爲你有同樣的問題。 –

+0

這是一個非常好的例子,但我相信它不會成爲我的問題的解決方案。我想要處理的數據是列出他們所附屬的濁度和植物。過濾的問題是,您一次只能顯示一個過濾器。那麼,我如何獲得x渦輪機列表中的屬於他們所屬工廠的數據? – lronhoj

回答

2

由屬於關聯關聯創建吸氣函數需要一個回調函數作爲參數。回調函數將把相關對象作爲它的第一個參數。

turbine.getPlant(function(Plant){ 
       console.log(Plant); 
      }); 

我會附上一個完整的工作示例,因爲這會讓我頭痛不已,並且可能會有其他人。

第一JSON數據:

{ 
"plants": [{ 
     "id": 1, 
     "name": "Plant1", 
     "notes": ["Note1", "Note2"] 
    }], 
"turbines": [ 
    { 
     "id": 11, 
     "plant_id": 1, 
     "name": "T41", 
     "notes": ["Turbine note 1", "Turbine note 2"] 
    }] 
} 

和JavaScript:

Ext.regApplication({ 
       name: "app", 
       launch: function() {} 
      }); 

app.models.Plant = Ext.regModel("Plant", { 
    fields: ["id", "name", "notes"], 
    proxy: { 
     type: 'ajax', 
     url: 'data.json', 
     reader: { 
      type: 'json', 
      root: 'plants' 
     } 
    } 
}); 

app.models.Turbine = Ext.regModel("Turbine", { 
    fields: ["id", "plant_id", "name", "notes"], 
    proxy: { 
     type: 'ajax', 
     url: 'data.json', 
     reader: { 
      type: 'json', 
      root: 'turbines' 
     } 
    }, 
    belongsTo: 'Plant' 
}); 

app.stores.plants = new Ext.data.Store({ 
    model: "Plant" 
}); 

app.stores.turbines = new Ext.data.Store({ 
    model: "Turbine", 
    autoLoad: { 
     callback: function(records) { 
      var turbine = records[0]; 

      turbine.getPlant(function(Plant){ 
       console.log(Plant); 
      }); 
     } 
    } 
});