Reputation: 230
I have a model called User which, when I do a Fetch, would ideally expect to receive a JSON object that looks like this:
{"UserId":"20","FirstName":"Test","SurName":"User"}
But the object that comes back, and that I need to work with, looks like this:
{
"Type": "Single",
"Error: "",
"Objects": {
"UserId":"20",
"FirstName":"Test",
"SurName":"User"
}
}
This is likely to be true for all of the models in my application.
What's the best way to tell the models to populate themselves from the Objects
object rather than the root object?
Apologies if my question isn't as clear as it could be: I'm a Backbone virgin and this is all new to me.
Upvotes: 0
Views: 205
Reputation: 35890
You need to override the parse
method of your model.
var FooModel = Backbone.Model.extend({
parse: function(response) {
//response is the raw JSON object. Whatever this method returns is used to populate the model.
return response.Objects;
}
});
See Backbone documentation for Model.parse.
Upvotes: 2