Serializing models
So far, the model data we used in our examples in the previous chapters are all simple data objects with attributes. However, there might be a case where the server is sending a different data format and you need to extract the essential part from it and apply it to the related model. For example, consider the following data:
{ "name": "John Doe", "email": "johndoe@example.com" }
Instead of sending the preceding data, the server returns the following data:
{ "user": { "name": "John Doe", "email": "johndoe@example.com" } }
This data cannot be applied directly to a model with the attributes name
and email
. If we call the fetch()
method on the model now, it will just add another attribute named user
to the model. The method that can help us overcome this issue is called parse()
. By default, this method just passes the server response and the model applies whatever it receives from the parse()
method. Here is how it is defined in Backbone.js
:
parse: function (resp...