I'm trying to create a record that has a hasMany/belongsTo association. The record gets created but it doesn't save...
Story model:
export default DS.Model.extend({
title: DS.attr('string'),
description: DS.attr('string'),
tasks: DS.hasMany('task', { async: true })
});
Task model:
export default DS.Model.extend({
title: DS.attr('string'),
story: DS.belongsTo('story', { async: true })
});
Component object:
actions: {
createNewTask(){
var taskTitle = this.get('newTaskTitle');
var tasks = this.get('story.tasks');
var story = this.get('story');
this.set('newTaskTitle', '');
this.sendAction('action', taskTitle, tasks, story);
this.send('toggleModal');
}
In the above, story=model and this.get('story.tasks') returns an empty array always.
route object:
export default Ember.Route.extend({
model: function(params){
return this.store.findRecord('story', params.id);
},
actions: {
createNewTask(newTaskTitle, tasks, story){
var newTask = this.store.createRecord('task', {
title: newTaskTitle
});
newTask.save().then(function(task){
//I think the issue is somewhere in here
tasks.addObject(task);
story.save();
});
}
}
Basically, the created task is not being saved with it's associated story...Any help would be great. Thanks!
EDIT This is the stored json from my local storage
{
"story":{
"records":{
"3jf2h":{
"id":"3jf2h",
"title":"Story1",
"description":"Story1",
"tasks":[
"6i03h"
]
}
}
},
"task":{
"records":{
"6i03h":{
"id":"6i03h",
"title":"T1",
"description":"T1",
"story":null
}
}
}
}
story.set('tasks', tasks).save()
– Gennady Dogaev