【问题标题】:Data model : Association getter returns undefined数据模型:关联 getter 返回未定义
【发布时间】:2011-10-19 19:32:17
【问题描述】:

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

下面附上相关代码:

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

【问题讨论】:

  • 阅读我的answer about models+associations+stores。我想你也有同样的问题。
  • 这是一个很好的例子,但我相信它不会解决我的问题。我想用我的数据做的是列出涡轮机和它们所连接的工厂。过滤的问题是您一次只能显示一个过滤器。那么我如何获得 x Turbines 的列表及其所属工厂的数据?

标签: extjs sencha-touch


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-11-16
    • 2019-07-31
    • 1970-01-01
    • 1970-01-01
    • 2013-05-25
    • 1970-01-01
    • 2016-04-24
    • 2021-08-23
    • 2020-07-03
    相关资源
    最近更新 更多