首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据模型:关联getter返回未定义

数据模型:关联getter返回未定义
EN

Stack Overflow用户
提问于 2011-08-04 17:18:16
回答 1查看 1.9K关注 0票数 0

我的问题在于无法通过关联检索数据。从控制台运行setup()之后,我希望firstTurbine.getPlant()返回相关的工厂,但它返回的却是undefined。我花了很多时间寻找解决方案,我可能找不到合适的地方。

相关代码如下:

代码语言:javascript
复制
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};
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-05 16:46:56

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

代码语言:javascript
复制
turbine.getPlant(function(Plant){
                console.log(Plant);
            });

我将附上一个完整的工作示例,因为这让我很头疼,可能也会让其他人头疼。

首先是json数据:

代码语言:javascript
复制
{
"plants": [{
        "id": 1,
        "name": "Plant1",
        "notes": ["Note1", "Note2"]
    }],
"turbines": [
    {
        "id": 11,
        "plant_id": 1,
        "name": "T41",
        "notes": ["Turbine note 1", "Turbine note 2"]
    }]
}

和javascript:

代码语言: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);
            });
        }
    }
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6938888

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档