我正在构建一个应用程序,您可以在其中保留多个任务列表以及每个任务项的注释。我可以毫无问题地创建任务,但是当我尝试创建/删除评论时,我得到“Uncaught TypeError: Cannot call method 'createRecord' of undefined”,这似乎暗示我没有正确访问评论模型到控制器依赖或具有模型关系的东西。谁能指出我正确的方向?
这是我的路线
App.Router.map(function() {
this.resource('lists');
this.resource('list' , {path: ':list_id'});
});
App.ApplicationRoute = Ember.Route.extend({
setupController : function(){
this.controllerFor('lists').set('model', this.store.find('list'));
this.controllerFor('task').set('model' , this.store.find('task'));
this.controllerFor('comment').set('model' , this.store.find('comment');
}
});
App.ListsRoute = Ember.Route.extend({
model : function(){
return this.store.find('list');
}
});
App.ListRoute = Ember.Route.extend({
model : function(params){
return this.store.find('list', params.list_id);
}
});
这是我的模型层次结构
App.List = DS.Model.extend({
tasks: DS.hasMany('task', {async : true})
});
App.Task = DS.Model.extend({
description: DS.attr('string'),
list: DS.belongsTo('list'),
comments : DS.hasMany('comment')
});
App.Comment = DS.Model.extend({
body : DS.attr('string'),
task : DS.belongsTo('task')
});
这是我的控制器(注意,控制器项目只是为了让我编辑每个单独的任务,所以如果你愿意,可以忽略它)
App.ListController = Ember.ObjectController.extend({
});
App.TaskController = Ember.ArrayController.extend({
needs : ['list'],
actions : {
addTask : function(){
var foo = this.store.createRecord('task', {
description : '',
list : this.get('content.id'),
comments : []
});
foo.save();
console.log('Task Created!');
}
}
});
App.ItemController = Ember.ObjectController.extend({
//code to edit or remove individual tasks
});
App.CommentController = Ember.ObjectController.extend({
needs : ['task'],
actions : {
save: function(newCommentBody) {
var foo = this.store.createRecord('comment',{
body: newCommentBody,
task : this.get('content.id')
});
task.save();
console.log('Comment Created!');
}
}
});