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.
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.
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.
<br> `therefore` 512 cannot be the number of reflexive relations defined on a set A.
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.
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);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With