2014-11-04 62 views
0

关联记录我有这样的设置:与Ember数据

App.MyModel = Em.Model.extend({ 
    someValue: DS.attr('string'), 
    parent : DS.belongsTo('mymodel',{async:true, inverse:'rooms'}), 
    rooms : DS.hasMany('mymodel', {async:true, inverse:'parent'}) 
}); 

App.MyRoute = Em.Route.extend({ 
    model:function(params){ 
    var parent = this.store.find('mymodel', params.parent_id); 
    return this.store.createRecord('mymodel',{parent:parent}); 
    } 
}); 

params.parent_id有我想要的ID,所以find()应该返回正确的记录。

然后someValue被绑定在模板的输入框中,输入后被调用的动作create

App.MyController = Ember.ObjectController.extend({ 
    actions:{ 
    create:function(){ 
     this.get('model').save(); 
    } 
    } 
}); 

但是,当数据被发送到服务器,只someValue有正确的数据,parentnull

我不知道错误是在模型定义中还是在我设置关系的方式中。

如何正确设置记录关系?

回答

0

find的承诺应解析为实际对象。

但是,在问题中使用它时不起作用。

有一种不同的方法,可以在需要的记录已经加载时使用。

model: function(params){ 
    return this.store.createRecord('mymodel', { 
    'parent': this.store.getById('mymodel', params.parent_id) 
    }); 
} 

这是有效的,因为我从已经加载父路由的路由。