我有一个视图,它在父元件下面呈现一个子视图循环。添加新的表单位于同一视图中。当我保存子对象时,我知道它已被推送到父对象子promise数组中。但是当我试图删除子对象时,视图被删除的对象没有更新,所以现在我保存父,子,然后重新加载父模型。
管理这种关系似乎有些过分。我错过了什么,或者这是目前在Ember Data中管理异步关系的正确方法。我目前正在使用Beta 8版本。
一些代码段:
父模型:
var parent = DS.model.extend({
name: DS.attr(),
children: DS.hasMany('parent/child', {async: true})
});
parent.reopenClass({
FIXTURES: [
{id: 1, name: "TEST", children: [1, 2]}
]
});
儿童模特:
var child = DS.model.extend({
name: DS.attr(),
parent: DS.belongsTo('parent')
});
child.reopenClass({
FIXTURES: [
{id: 1, name: "TEST", parent: 1},
{id: 2, name: "TEST", parent: 1}
]
});
插入新子记录的屏幕与父模型位于同一路径中,我有以下两个操作:
actions:
{
save: function()
{
var self = this;
var child = this.store.createRecord('parent/child', {name: 'test', parent: this.get('model'});
child.save().then(function(record)
{
self.get('content.children').pushObject(record);
parent.save().then(function(){
self.get('model').reload();
});
});
},
delete: function(id)
{
this.store.find('parent/child', id).then(function(child)
{
child.deleteRecord();
child.save();
});
}
}
答案 0 :(得分:1)
save: function()
{
var self = this;
var child = this.store.createRecord('parent/child', {name: 'test', parent: this.get('model'});
child.save().then(function(record)
{
self.get('children').then(function(children){
// making sure children has resolved before attempting to use it
children.pushObject(record);
parent.save();//.then(function(){
// this shouldn't be necessary
// self.get('model').reload();
// });
});
});
},