【发布时间】: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);
}
});
在控制台中检查父级时,我发现:
-
console.log(parent.data.children[0]):
控制台输出:
Object
id: 3
validFrom: -1767225600000
__proto__: Object
-
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 很抱歉缺少信息。我已经对我的问题进行了大量编辑,并希望它更清楚(我也为我清理了一些东西)。