Reputation: 4191
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
}
}
}
}
Upvotes: 0
Views: 248
Reputation: 4191
After messing around with things I came to a solution, but i'm not sure if its the best solution...
In my story route:
actions: {
createNewTask(newTaskTitle, newTaskDesc, tasks, taskType, story){
var newTask = this.store.createRecord('task', {
title: newTaskTitle,
description: newTaskDesc,
status: 'Not Started',
type: taskType
});
newTask.save().then(function(task){
tasks.addObject(task);
story.save().then(function(){
task.set('story', story);
task.save();
});
});
}
}
What I did was set a promise on the saved story and set the task's story to the newly saved story.
Even though this solved my problem, is this the proper way of doing things?
Upvotes: 1