Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Response from the server when saving a record to set the id ember-data

After I save the record using selection.save() where selection is my model, id is not being set on the model. My current server response is same as the json for show. i.e: selection: {id: <id>} and other fields. Buy id is not set in the ember data local storage. I'm using the default RESTAdapter and Rails as backend. (I use jbuilder for JSON)

model is MatchPlayerSelection and once I save the record, this is the response I send back:

{"match_player_selection":{"id":41298,"user":3,"scheduledMatch":133,"matchPlayerScores":[3697,3696,3694,3698,3704,3719,3712,3709,3717,3707,3714],"captain":3694,"viceCaptain":3709}
like image 794
Gautham Avatar asked Oct 17 '13 13:10

Gautham


1 Answers

How are you testing for the id getting set? The save function is async and returns a promise, so you need to resolve the promise before checking the id.

This will NOT work:

selection.save();
console.log( selection.get('id') );

This WILL work:

selection.save().then(function(savedSelection){
  console.log( savedSelection.get('id') );
});
like image 124
Jeremy Green Avatar answered Nov 11 '22 23:11

Jeremy Green