Emberjs: Cannot createRecord with Ember Data 1.0.0 beta

删除回忆录丶 提交于 2019-12-06 14:27:58

Yes you need pushObject in the list controller.

I would do the addTask like this, now almost every method in ember data return a promise

App.TaskController = Ember.ArrayController.extend({
  needs : ['list'],
  listController:Ember.computed.alias('controllers.list'),
  isEditing : false,
  actions : {
    addTask : function(){
        var listId = this.get('listController.model.id'),
        list = this,
        store = this.get('store');
        console.log('listId',listId);
        store.find('task').then(function(tasks){
            var newId = tasks.get('lastObject.id') + 1,
            newTask = store.createRecord('task', { 
                id:newId,
                description : '',
                list : listId,
                comments : []  
           });
            newTask.save().then(function(newTaskSaved){
                list.pushObject(newTaskSaved);
                console.log('Task Created!');
            });
        });
    },
    edit : function(){
       this.set('isEditing', true);
    },
    doneEditing : function(){
        this.set('isEditing', false);
    }
  }
});

I think seerting id's properly is very important, here with fixtures I do with a find, but with the rest adapter would be the backend who assign the id and set it in the response

JSFiddle http://jsfiddle.net/W6QWj/2/

Okay, so I've answered my own question. I was missing the pushObject() method. Here is another way to do things. Although I'm not sure if it's the best practice since it does throw the error "Assertion failed: You can only add a 'list' record to this relationship "

App.ListController = Ember.ObjectController.extend({
  actions:{
    addTask : function(){
      var foo = this.store.createRecord('task', { 
        description : '',
        list : this.get('content.id'),
        comments : []  
      });
      this.get('tasks').pushObject(foo);
      foo.save();
    }
  }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!