Friday, May 10, 2024
 Popular · Latest · Hot · Upcoming
172
rated 0 times [  173] [ 1]  / answers: 1 / hits: 25639  / 12 Years ago, mon, august 13, 2012, 12:00:00

I'm trying to use Backbone with an API.



The default API response format is:



{
somemetadatas:xxx ,
results:yyy
}


Whether it's a fetch for a single model or a collection.



So as far as I know I can override the Backbone parse function with:



parse: function (response) {
return response.results;
},


But I've seen in the documentation:




parse collection.parse(response)



parse is called by Backbone whenever
a collection's models are returned by the server, in fetch. The
function is passed the raw response object, and should return the
array of model attributes to be added to the collection. The default
implementation is a no-op, simply passing through the JSON response.
Override this if you need to work with a preexisting API, or better
namespace your responses. Note that afterwards, if your model class
already has a parse function, it will be run against each fetched
model.




So if I have a response for a collection fetch like that:



{
somemetadatas:xxx ,
results:[user1,user2]
}


The first parse function on the collection will extract [user1,user2].



But the doc says:




Note that afterwards, if your model class
already has a parse function, it will be run against each fetched
model.




So it will try to find response.results; on user1 and user2



I need both parse functions on the model and collection because both model and collection datas will be under the result attribute.



But if i fetch on a collection, I don't want the model parse function to be used againt a single array element.






So is there a solution to this problem?



I think of a solution where my collection parse function will transform something like this:



{
somemetadatas:xxx ,
results:[user1,user2]
}


into something like this:



[ {results.user1} , {results.user2} ]


So that the model parse function will not fail on a collection fetch.
But it's a bit hacky... is there any elegant solution to this problem?






By the way, as my API will always produce results of this form, is it possible to override by default the parse function of all my models and collections? (Sorry i'm a JS noob...)


More From » backbone.js

 Answers
55

You could test if the data you receive is wrapped by a results member and react accordingly. For example,



var M = Backbone.Model.extend({
parse: function (data) {
if (_.isObject(data.results)) {
return data.results;
} else {
return data;
}
}
});


And a Fiddle http://jsfiddle.net/9rCH3/



If you want to generalize this behavior, either derive all your model classes from this base class or modify Backbone's prototype to provide this function :



Backbone.Model.prototype.parse = function (data) {
if (_.isObject(data.results)) {
return data.results;
} else {
return data;
}
};


http://jsfiddle.net/9rCH3/1/


[#83668] Sunday, August 12, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
mireyag

Total Points: 73
Total Questions: 107
Total Answers: 85

Location: Ukraine
Member since Sun, Dec 13, 2020
3 Years ago
mireyag questions
Sun, Aug 15, 21, 00:00, 3 Years ago
Wed, Dec 16, 20, 00:00, 3 Years ago
Tue, Sep 1, 20, 00:00, 4 Years ago
Sun, Jul 5, 20, 00:00, 4 Years ago
;