0
votes

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
         }
      }
   }
}
1
What do you mean by "created task is not being saved with it's associated story"? Do you have some errors or what?Gennady Dogaev
@GennadyDogaev I edited my question with the json being stored. Whenever I create a task, it gets added into the story's tasks array, but for the newly created task the story is nullJshoe523
So you are using local storage and looking there. But, I think that everything is okay here and when ember will load data from storage, it will handle relationship.Gennady Dogaev
@GennadyDogaev it's just everytime I try to create a new task, it overwrites the existing task instead of adding to it...Jshoe523
try story.set('tasks', tasks).save()Gennady Dogaev

1 Answers

1
votes

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?