Reputation: 71
I have an app that keeps multiple task lists. Each task list has multiple tasks. Each task has multiple comments.
After updating to the new Ember Data, I had to scrap my record creation code. Currently I have this, which doesn't work. Though it doesn't throw any errors, my model does not seem to be updating.
App.TaskController = Ember.ArrayController.extend({
needs : ['list'],
isEditing : false,
actions : {
addTask : function(){
var foo = this.store.createRecord('task', {
description : '',
list : this.get('content.id'),
comments : []
});
foo.save();
console.log('Task Created!');
},
edit : function(){
this.set('isEditing', true);
},
doneEditing : function(){
this.set('isEditing', false);
}
}
});
Does anyone know how to create a new task (as well as how to create new comments) in this context?
See fiddle here : http://jsfiddle.net/edchao/W6QWj/
Upvotes: 2
Views: 933
Reputation: 71
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();
}
}
});
Upvotes: 1
Reputation: 2520
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/
Upvotes: 2