【问题标题】:ExtJS MVC belongsTo/hasMany accessing childrenExtJS MVC belongsTo/hasMany 访问子节点
【发布时间】:2013-06-25 06:13:43
【问题描述】:

我正在使用带有 ExtJS MVC 功能的 ExtJS 4.0.7。我有一个父模型 hasMany 子代 belongsTo 父模型。

访问孩子的正确方法是什么?如果我通过parent.children().data.items[0].data(而不是parent.data.children[0]),则有一个不需要的属性MyApp.model.parent_id。我还注意到日期存储方式之间的差异。

首先,这里是父级的定义。所以父母会有很多孩子。

Ext.define('MyApp.model.Parent', {
    extend : 'Ext.data.Model',
    fields : [ 'id' ],
    hasMany : [ {
        model : 'MyApp.model.Child',
        name : 'children'
    }],
    proxy : {
        type : 'direct',
        api : {
            create : Parents.create,
            read : Parents.read,
            update : Parents.update,
            destroy : Parents.destroy
        }
    }
});

每个孩子都属于父母。

Ext.define('MyApp.model.Child', {
    extend : 'Ext.data.Model',
    fields : [ 'id', {
        name : 'validFrom',
        type : 'date',
        dateFormat : 'time'
        } ],
    belongsTo : 'Parent'
});

我在我的控制器中加载了一个父对象:

this.getParentModel().load(id, {
    scope : this,
    success : function(parent) {
                    // the party looks good here when logged to console
                    console.log(parent);
        this.updateStore(parent);
    }
});

在控制台中检查父级时,我发现:

  1. console.log(parent.data.children[0]):

控制台输出:

Object
id: 3
validFrom: -1767225600000
__proto__: Object
  1. console.log(parent.children().data.items[0].data):

控制台输出:

Object
id: 3
MyApp.model.parent_id: 209 // why is that here?
validFrom: Thu Jan 01 1914 01:00:00 GMT+0100 (Central European Standard Time)
__proto__: Object

【问题讨论】:

  • 虽然我确信您的问题对您来说很清楚,但使用显示的小代码很难理解/回答。能否分享一下这两个模型的定义?
  • @Izhaki 很抱歉缺少信息。我已经对我的问题进行了大量编辑,并希望它更清楚(我也为我清理了一些东西)。

标签: extjs4 extjs-mvc


【解决方案1】:

如果我理解正确,我想你想要:

var someRecordId = 'idProperty';
var record = Ext.getStore('Parent').getById(someRecordId);

// this returns all hasMany for the name of of the property i.e. name: 'child'

record.child();  

这将为父记录返回子项中的所有项目数据(基于模型定义)。它应该只包括子模型定义中的那些字段。所以 parent_id 将不包括在内。如果您尝试检索 Child 的所有记录,那么您只需以与任何其他存储相同的方式获取它们。

【讨论】:

    猜你喜欢
    • 2013-11-03
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多