Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to save to a hasMany relationship using ember data

I have some related models in my ember.js app (using Ember 1.0 and EmberData 1.0 RC2):

App.List = DS.Model.extend({
    listName : DS.attr( ),
    cards : DS.hasMany( 'card', { async : true } )
});

and

App.Card  = DS.Model.extend({
    description : DS.attr(  ),
    list : DS.belongsTo( 'list' )
});

I'm using the following code to save models and add them to a hasMany relationship.

createCard : function(){
    var list = this.get( 'model' ),
        card ;

    card = this.store.createRecord( 'card', {
        description : this.get( 'cardDescription' ),
        list : list
    } );

    card.save().then( function(){
        var cards = list.get( 'cards' );

        cards.then( function(){
            cards.pushObject( card );
            list.save();
        } );
    } );

    this.set( 'cardDescription', '' );
}

I'm running into intermittent issues when saving the parent of the hasMany collection. Sometimes the cards get added to the lists collection properly ( the lists has an array of card id's) and sometimes the cards get added incorrectly ( the lists has an array of card objects) and sometimes the relationship gets lost all together (the lists contains no array of cards).

These symptoms lead me to think that its an async issue or that i'm using the promises incorrectly when saving the objects.

like image 825
bittersweetryan Avatar asked Sep 18 '13 20:09

bittersweetryan


People also ask

How do you save Ember?

Call save() on any instance of Model and it will make a network request. Ember Data takes care of tracking the state of each record for you. This allows Ember Data to treat newly created records differently from existing records when saving. By default, Ember Data will POST newly created records to their type URL.

What are polymorphic models in Ember?

Polymorphism. Polymorphism is a powerful concept which allows a developer to abstract common functionality into a base class. Consider the following example: a user with multiple payment methods. They could have a linked PayPal account, and a couple credit cards on file.

Which of the following Cannot be a reflexive relationship in Ember?

<br> `therefore` 512 cannot be the number of reflexive relations defined on a set A.

What is store in Ember?

One way to think about the store is as a cache of all of the records that have been loaded by your application. If a route or a controller in your app asks for a record, the store can return it immediately if it is in the cache.


1 Answers

This looks pretty much the same as I have used here. The only difference I can see is that I pushed the child model to the parent in one step (which I doubt makes a difference) and also had the comment (which I guess is the card in your case) as the subject of the promise inside the 'then' function, maybe that makes a difference?

var post = this.get('controllers.post.content');
var comment = this.get('store').createRecord('comment', { post: post, text: this.get('text') });
comment.save().then(function(comment){
  post.get('comments').pushObject(comment);
});
like image 119
Ian Avatar answered Jan 26 '23 08:01

Ian