Emberjs:从依赖控制器创建/删除记录



我正在构建一个应用程序,您可以在其中保留多个任务列表,其中包含每个任务项的注释。我可以毫无问题地创建任务,但是当我尝试创建/删除注释时,我收到"未捕获的类型错误:无法调用未定义的方法'createRecord'",这似乎意味着由于控制器依赖项或模型关系,我没有正确访问注释模型。 谁能指出我正确的方向?

这是我的路线

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!');
    }  
  }     
});

抱歉耽搁了,美味我。 Ember Data 的大部分语法都已更改,从 1.0.0.beta.1 开始。 您可能需要查看过渡文档以获取更多信息:https://github.com/emberjs/data/blob/master/TRANSITION.md

以下是我可以立即发现的一些事情。

查找记录

老路

App.List.find();

新方式

this.store.find('list');

this.store.find('list', someListId);

创建记录

老路

App.List.createRecord({...});

新方式

this.store.createRecord('list', {...});

我希望这有所帮助。 随时发布后续问题。

最新更新