2011-08-04 15 views
0

私の問題は、関連付けによってデータを取得できないことにあります。 コンソールからsetup()を実行した後、firstTurbine.getPlant()は関連するプラントを返すと予想されますが、未定義を返します。 私はおそらく正しい場所を探していないだろう解決策を探して多くの時間を費やしました。 belongsTo関連によって作成データモデル:Association getter returns 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-models-with-associations-and-stores/6888446#6888446)を参照してください。私はあなたに同じ問題があると思います。 –

+0

それは非常に良い例ですが、私はそれが私の問題の解決策ではないと信じています。私が自分のデータを使ってやりたいことは、彼らが付けられている濁りとプラントのリストを作ることです。フィルタリングの問題は、一度に1つのフィルタしか表示できないことです。では、どのようにして、所属プラントのデータがあるxタービンのリストを取得できますか? – lronhoj

答えて

2

ゲッター関数は、引数として、コールバック関数をとる:

関連するコードを以下に取り付けられています。コールバック関数は、最初の引数として関連オブジェクトを持ちます。

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

これは頭痛の多く私のコストしているし、他の人のためのaswellがあるかもしれないので、私は完全な実施例を添付します。

最初の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); 
      }); 
     } 
    } 
});