![](/img/trans.png)
[英]What is the Ember way of converting retrieved Ember Data records into plain objects?
[英]In Ember data, what is the best way to form association between two objects?
假设某些DS.Model类Leaf
属于To类Tree
,并且其中一个Tree
hasMany leaves
,即:
App.Leaf = DS.Model.extend({
tree: DS.belongsTo('App.Tree'),
});
App.Tree = DS.Model.extend({
leaves: DS.hasMany('App.Leaf'),
})
到目前为止,我正在手动操作Tree
的leaves字段:
tree = App.store.create( App.Tree )
leaf = App.store.create( App.Leaf )
tree.get('leaves').pushObject( leaf )
App.store.commit()
现在,这似乎可行,但是事情变得奇怪了:
当我检查leaf的tree字段时,我看到其中有一个App.Tree实例,并且id与tree匹配:
leaf.get('tree').get('id') // outputs 1
tree.get('id') // outputs 1
到目前为止,还可以。 现在,我检查树的leaves字段,我假设它是一个Ember可变数组,我看到了:
branch.get('leaves').content // outputs [ 2 ]
leaf.get('id') // outputs 1
因此,我假设叶子的可变数组存储的是叶子ID的数组,但其ID与叶子实例的ID不匹配。
请注意,当叶子的ID为2时,将其存储在branch.leaves.content字段中,值为4,如果叶子的ID为3,则存储的ID为6,依此类推。
在您的示例中,一切工作正常,访问内容变量branch.get('leaves')
记录数组将返回对象的clientId
的数组。
但是,这是例外,因为以任何其他方式访问属性都将透明地访问对象本身。
在您的情况下,如果要使用ID,请使用branch.get('leaves').mapProperty('id')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.