其中一个hasMany关系没有POST回服务器。您应该如何建立双向关系模型?
以下是相关对象:
Encompass.Selection = DS.Model.extend({
text: DS.attr('string'),
submission: DS.belongsTo('submission', {inverse: 'selections'}),
});
Encompass.Submission = DS.Model.extend({
shortAnswer: DS.attr('string'),
selections: DS.hasMany('selection'),
testing: DS.hasMany('folder'),
workspaces: DS.hasMany('workspace'),
});
还有一个控制器动作:
testing2: function() {
var submission = this.get('model');
console.log(submission.get('selections.length'));
var newSelection = this.get('store').createRecord('selection', {
text: 'testing2 selection' + new Date().getMilliseconds(),
submission: submission,
coordinates: 'bogus coords',
workspace: this.get('currentWorkspace')
});
//newSelection.save();
console.log(submission.get('selections.length'));
submission.save();
},
当我点击测试2操作时,控制台会首先显示该提交中有1个选择,然后是第二个。 save()
方法触发了一个 post 请求,但它缺少了 selections 对象:
{
submission: {
shortAnswer: "short",
testing: [],
workspaces: ["5271d0147205f15e31000001"]
}
}
我尝试过删除和添加反向映射。奇怪的是其他的 hasMany 关系都能正常工作。
我发现唯一有效的方法是从 Selection 中删除 submission 字段。