admin管理员组

文章数量:1181449

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...)

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...)

Share Improve this question edited Aug 13, 2012 at 17:03 mu is too short 435k71 gold badges857 silver badges818 bronze badges asked Aug 13, 2012 at 16:35 Sebastien LorberSebastien Lorber 92.1k73 gold badges298 silver badges428 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 28

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/

Parse also must be implemented in the Collection.

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

http://backbonejs.org/#Collection-parse

本文标签: javascriptOverriding backbone39s parse functionStack Overflow