Ember Data中的类继承有许多关联 [英] Class inheritance in Ember Data hasMany associations
问题描述
Ember代码中的示例:
var Person = DS.Model.extend({
name:DS.attr('string'),
animals:DS.hasMany('动物')
});
var Animal = DS.Model.extend({
name:DS.attr('string'),
owner:DS.belongsTo('Person')
});
var Dog = Animal.extend({
isBarking:DS.attr('boolean')
});
var Cat = Animal.extend({
isMeowing:DS.attr('boolean')
});
var fred = Person.createRecord({name:'Fred'});
var ed = Dog.createRecord({name:'Ed',isBarking:true});
//给出:
//错误:断言失败:您只能添加动物记录到这种关系。
fred.get('animals')。pushObject(ed);
我希望能够将猫和狗实例添加到动物
Mongoid提供的Ruby / Rails的一个例子如下:
http://mongoid.org/en/mongoid/docs/ document.html#继承
我知道Mongoid的继承实现(一个自定义的 _type
属性对于每个记录)可能对Ember Data来说太具体了。
如果有人可以给我一个提示,上面是否可以使用Ember Data,如果是这样,我怎么可以实现这个,这将是非常感谢。如果这是Ember Data不支持的,那么这也是我想要的答案。
我正在运行Ember 1.0.0 -pre4和几天前的自定义Ember Data构建。
多态性尚未正式支持,但有一个常见的拉取请求,提供您正在寻找的功能。
I'm trying to get Ember Data working with a hasMany association with class inheritance.
Example in Ember code:
var Person = DS.Model.extend({
name: DS.attr('string'),
animals: DS.hasMany('Animal')
});
var Animal = DS.Model.extend({
name: DS.attr('string'),
owner: DS.belongsTo('Person')
});
var Dog = Animal.extend({
isBarking: DS.attr('boolean')
});
var Cat = Animal.extend({
isMeowing: DS.attr('boolean')
});
var fred = Person.createRecord({name: 'Fred'});
var ed = Dog.createRecord({name: 'Ed', isBarking: true});
// This gives:
// Error: assertion failed: You can only add records of Animal to this relationship.
fred.get('animals').pushObject(ed);
I would like to be able to add Cat and Dog instances to the animals
for each Person.
An example of how it might be implemented is given by what Mongoid provides for Ruby/Rails:
http://mongoid.org/en/mongoid/docs/documents.html#inheritance
I'm aware that Mongoid's inheritance implementation (a custom _type
attribute for each record) will probably be too specific for Ember Data to implement.
If anyone could give me a hint whether the above is possible with Ember Data, and if so, how I could implement this, it would be greatly appreciated. If this is something that won't be supported by Ember Data at all, that's also an answer I'd like to have.
I'm running Ember 1.0.0-pre4 and a custom Ember Data build from a few days ago.
Polymorphism isn't officially supported yet, but there is a standing pull request that proposes exactly the feature you are looking for.
这篇关于Ember Data中的类继承有许多关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!