数据模型:关联getter返回undefined

时间:2011-08-04 09:18:16

标签: extjs sencha-touch

我的问题在于无法通过关联检索数据。 从控制台运行setup()后,我希望firstTurbine.getPlant()返回关联的工厂,但它返回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};
}

1 个答案:

答案 0 :(得分:2)

belongsTo关联创建的getter函数将回调函数作为参数。回调函数将相关对象作为其第一个参数。

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