我还没有通过搜索找到满意的答案,所以我想我会在这里问。在hasMany中创建子记录与Ember.Js的关系
我正在使用Ember.Js,Ember-Data和Ember-Firebase-Adapter,并试图创建一个CRUD应用程序,该应用程序将创建一个父记录,然后创建一个后续的子记录到所述父记录。
(注意DS.Firebase.LiveModel是火力地堡适配器相当于DS.Model/Ember.Model的)
这里有我的模式,改变为通用的帖子/评论类型
App.Post = DS.Firebase.LiveModel.extend({
name: DS.attr('string'),
body: DS.attr('string'),
date: DS.attr('date'),
comments: DS.hasMany('App.Comment', {embedded: 'always'})
});
App.Comment = DS.Firebase.LiveModel.extend({
message: DS.attr('string'),
timestamp: DS.attr('string'),
post: DS.belongsTo('App.Post', {key: "post_id"})
});
(?如若我post_id
= post
)
这里是我创建Comments
路线:
App.PostsCommentRoute = Ember.Route.extend({
setupController: function(controller) {
controller.set('content', App.Comment.find());
}
});
这是我为PostsCommentRoute
控制器:
App.PostsCommentController = Ember.ObjectController.extend({
newMessage: null,
newTimestamp: null,
saveComment: function() {
App.Pbp.createRecord({
message: this.get('newMessage'),
timestamp: this.get('newTimestamp')
})
App.store.commit();
this.set('newMessage', null);
this.set('newTimestamp', null);
}
});
我想我可能会丢失串行?我已经阅读了addArray
上的几件事情,但是我试图插入的东西并不是很有成效。因为我的评论创建的很好,但是他们在我的JSON中并没有关联到帖子。
有没有办法让创建的Comments
找到相关的Post_Id
,然后在创建时关联到上述Post
?所以我的Post
JSON有一组Comment_Ids
然后允许他们与帖子一起显示?
任何帮助,或与良好的例子的链接将不胜感激。我知道这是一个比较简单的窘境但我已经粘贴了有一段时间了:P