覆盖主干的解析功能 [英] Overriding backbone's parse function

查看:21
本文介绍了覆盖主干的解析功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 API 使用 Backbone.

I'm trying to use Backbone with an API.

默认的 API 响应格式为:

The default API response format is:

{
somemetadatas:xxx , 
results:yyy
}

无论是获取单个模型还是集合.

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

据我所知,我可以使用以下命令覆盖 Backbone parse 函数:

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

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

但我在文档中看到:

解析 collection.parse(response)

parse 每当集合的模型由服务器在 fetch 中返回.这函数被传递给原始的 response 对象,并且应该返回要添加到集合中的模型属性数组.默认的实现是一个空操作,只是通过 JSON 响应.如果您需要使用预先存在的 API,或者更好,请覆盖它命名您的响应.请注意,之后,如果您的模型类已经有一个 parse 函数,它将针对每个提取的模型.

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]
}

集合上的第一个 parse 函数将提取 [user1,user2].

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

但是文档说:

注意之后,如果你的模型类已经有一个 parse 函数,它将针对每个获取的模型.

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

所以它会尝试在 user1user2

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]
}

变成这样:

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

这样模型解析函数就不会在集合获取时失败.但它有点hacky......这个问题有什么优雅的解决方案吗?

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?

顺便说一下,由于我的 API 总是会产生这种形式的结果,是否可以默认覆盖我所有模型和集合的 parse 函数?(对不起,我是一个 JS 菜鸟...)

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

推荐答案

您可以测试您收到的数据是否由 results 成员包装并做出相应的反应.例如,

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;
        }
    }
});

还有一把小提琴 http://jsfiddle.net/9rCH3/

如果你想概括这个行为,要么从这个基类派生所有的模型类,要么修改 Backbone 的原型来提供这个功能:

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/

这篇关于覆盖主干的解析功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆