Posts.create({'body':post_body});
When I call that, Backbone will hit my sever with an AJAX post request, creating that post. My server will then return a JSON with the “full” post.
Perfect! But now, I want the newly created model to to have the full data. In other words, I don’t want it to only have the body attribute. (all my other models have other data).
My question is:
- will backbone automatically update the model with the “full” data because my server returned that full JSON?
- if not, how can I get Backbone to update that model so its data is full?
Edit: I did this, and it seems like Backbone automatically uses the data returned as the new model. Can someone confirm?
success:function(post){
console.log(post.toJSON()); //Yay! latest version.
},
Yes, the model will be updated with any additional info your server returns.
This is because under the model’s
createmethod, there is a call tosave, which intern callsmodel.setwithin it, in order to update the model with any amended (or new attributes).As the source code comment for this method states: