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.