Ember.js 如何将子记录添加到现有父记录?
我一直在谷歌上搜寻关于这个主题的一些提示,但这些信息充其量只是零散的 我正在尝试创建一个新的子记录(Ember.js 如何将子记录添加到现有父记录?,ember.js,ember-data,ember-model,Ember.js,Ember Data,Ember Model,我一直在谷歌上搜寻关于这个主题的一些提示,但这些信息充其量只是零散的 我正在尝试创建一个新的子记录(Comment)并将其保存到现有的父记录(Post)。我使用余烬模型,而不是余烬数据,但任何提示或指针将不胜感激 目前,我已经成功地创建了一个新的嵌入式注释,但是只有在使用新的Post记录创建时才是。因此: 如何加载/检索当前加载的Post(父记录)以对其应用注释(子记录)? 我一直在使用需求:和this.controllerFor和this.modelFor阅读控制器依赖项,以便访问另一个控制器
Comment
)并将其保存到现有的父记录(Post
)。我使用余烬模型,而不是余烬数据,但任何提示或指针将不胜感激
目前,我已经成功地创建了一个新的嵌入式注释
,但是只有在使用新的Post
记录创建时才是。因此:
如何加载/检索当前加载的Post
(父记录)以对其应用注释(子记录)?
我一直在使用需求:
和this.controllerFor
和this.modelFor
阅读控制器依赖项,以便访问另一个控制器/模型的内容
,但无法将这些内容连接到一起,形成有意义的内容
无论如何,以下是我将我的应用程序代码缩减到的内容,希望我能够找到正确的方法来实现这一点
路线
我删除了所有其他资源和路由,所以剩下的是App.Post
、App.PostIndex
、和App.Comments
。我认为我的路线是这里的问题,我假设我没有正确地实现在我的注释路线中使用加载的Post
记录的方法
App.IndexRoute = Ember.Route.extend({
model: function() {
return App.Post.find();
},
setupController: function(controller, model) { // I'm not certain if this
controller.set('content', model); // setupController is needed?
}
});
App.PostRoute = Ember.Route.extend({
model: function(params) {
return App.Post.find(params.post_id);
},
setupcontroller: function( controller, model) { // again, unsure if this
this.controllerFor('post').get('comments'); // is correct.
controller.set('content', comments);
}
});
App.CommentsRoute = Ember.Route.extend({
afterModel: function() {
this.set('post', this.modelFor('post'));
},
setupcontroller: function( controller, model) {
this.controllerFor('post').get('comments');
controller.set('content', comments);
}
});
控制器
这是我当前的Comments Controller,它可以创建一个新的带有嵌入的注释的Post
。我发现并得到了许多创建注释的例子,但似乎没有一个适合我。基本上,我正在努力将var post=…
定义为当前加载的记录。我已经实现了各种方法,试图尝试尝试尝试错误。迄今为止,我已尝试:
var post=App.post.create()代码>,返回未定义的属性,因为这将创建新记录。然而,我给了它一个机会,因为我看到的每一个与此相关的例子都定义了他们的记录
var post=this.get('post')代码>,在未定义的情况下返回无法调用“get”。我尝试使用这种方法在评论
控制器和帖子
控制器上定义我当前的帖子
var post=this.get('controllers.post.content)代码>,从我正在使用的后端返回“循环错误”
var post=App.post.find()代码>,在未定义的情况下返回无法调用“get”
var post=App.post.find(1)代码>,再次在未定义的情况下返回无法调用“get”。我想我应该试一试,因为这是人们反复提供的例子之一。我使用的后端将其自己的ID应用于每个记录,我不确定是否能够/如何让.find()
方法使用动态ID值并仅检索我刚刚加载的模型
我猜我没有正确设置路由和控制器依赖项
如果有人有建议,相关链接,或修复我将非常感激
这个(看似简单的)问题/用例让我现在束手无策。试试这个(beta 2之前的作品):
余烬数据Beta 2及更高版本:
App.CommentsController = Ember.ArrayController.extend({
needs: ["post"],
actions: {
addComment: function() {
var post = this.get('controllers.post');
var comment = this.get('store').createRecord('comment', {
message: 'static message',
post: post
});
comment.save().then(function() {
post.addObject(comment);
// You may or may not need to save your post, too. In my case my backend handles
// the inverses of relationships (if existing), so there's no need. We still need
// to do this for Ember, though
});
}
}
});
您使用的是什么版本的余烬数据?谢谢您的帮助,我的评论路线是否正确设置以加载当前帖子?另外,我使用的是余烬模型,而不是数据。
App.CommentsController = Ember.ArrayController.extend({
needs: "post",
actions: {
addComment: function() {
var post = App.Post.create({
title: 'static post title'
});
post.get('comments').create({
message: 'static message'
});
post.save();
}
}
});
App.CommentsController = Ember.ArrayController.extend({
actions: {
addComment: function() {
this.content.createRecord({
message: 'static message'
});
}
}
});
App.CommentsController = Ember.ArrayController.extend({
needs: ["post"],
actions: {
addComment: function() {
var post = this.get('controllers.post');
var comment = this.get('store').createRecord('comment', {
message: 'static message',
post: post
});
comment.save().then(function() {
post.addObject(comment);
// You may or may not need to save your post, too. In my case my backend handles
// the inverses of relationships (if existing), so there's no need. We still need
// to do this for Ember, though
});
}
}
});