多对多关系尚不支持烬数据。目前,一种可能的解决方法是手动管理连接表。
A = DS.Model.extend({
abs: DS.hasMany('Ab'),
bs: function() {
return this.get('abs').getEach('b');
}
});
Ab = DS.Model.extend({
a: DS.belongsTo('A'),
b: DS.belongsTo('b')
});
B = DS.Model.extend({
abs: DS.hasMany('Ab'),
bs: function() {
return this.get('abs').getEach('a');
}
});
这只是一个起点。你需要再自定义您的模型和适配器,以发送/接收的工作方式
例如/坚持记录,在我们的应用程序,我们引入的hasMany关系内的{ includedJoin: true }
选项,并宣布将加入表作为一个JoinModel
A = DS.Model.extend({
abs: DS.hasMany('Ab', {includeJoin: true}),
...
});
DS.JoinModel = DS.Model.extend();
Ab = DS.JoinModel.extend({
... belongsTo relationships ...
});
然后在适配器,我们覆盖了创建/更新/以忽略在店里,我们在连接表的生命周期
createRecords: function (store, type, records) {
if (!DS.JoinModel.detect(type)) {
this._super(store, type, records);
}
}
最后,在串行删除方法乘坐addHasMany
函数以便将加入数据作为父模型中的嵌入式ID发送到服务器。
addHasMany: function (hash, record, key, relationship) {
var
options = relationship.options,
children = [];
//we only add join models, use of `includeJoin`
if (options.includedJoin) {
record.get(relationship.key).forEach(function (child) {
children.pushObject(child.toJSON({
includeId: true
}));
});
hash[key] = children;
}
}
服务器端,我们使用Rails与ActiveModelSerializer,所以只能小技巧 - 定制就是当我们更新父模型,我们手动管理连接关系,创建/删除连接表项。
在最新版本的Ember Data中,您可以使用App.ApplicationSerializer = DS.ActiveModelSerializer.extend({});'使用下划线的外键。 – rxgx