jeudi 20 août 2015

Emberjs create record with hasMany/belongsTo

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!




Aucun commentaire:

Enregistrer un commentaire