2011-08-04 4 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

그것은 아주 좋은 예입니다. 그러나 저는 제 문제에 대한 해결책이 될 수 없다고 생각합니다. 내 데이터로 수행하고자하는 것은 탁월함과 식물이 첨부 된 목록을 작성하는 것입니다. 필터링 문제는 한 번에 하나의 필터 만 표시 할 수 있다는 것입니다. 그렇다면 계열 공장의 데이터가있는 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"] 
    }] 
} 

그리고 자바 스크립트 :

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); 
      }); 
     } 
    } 
});