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

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

问题描述

我试图使用与骨干的API。

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.

所以,据我知道我可以覆盖骨干解析函数:

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

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

不过,我在文档:

解析 collection.parse(响应)

解析由骨干每当称为
  集合的模型是由服务器返回,在取。该
  函数传递的原始响应对象,应该返回
  模型的阵列属性被添加到集合。默认
  实现是一个空操作,只需通过JSON响应。
  重写此如果你需要用preexisting API工作,或更好
  命名空间的响应。请注意,以后,如果你的模型类
  已经有一个解析的功能,它会针锋相对牵强运行
  模型。

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

在集合的第一个解析函数将提取 [用户1,用户2]

但医生说:

请注意,以后,如果你的模型类
  已经有一个解析的功能,它会针锋相对牵强运行
  模型。

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

因此​​,将设法找到 response.results; USER1 user2的

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

我既需要解析的模型和收集功能,因为这两个模型和收藏DATAS将结果属性下。

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

但是,如果我对某个集合取,我不想againt单个数组元素使用的模型解析功能。

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

因此​​,该模型解析功能将无法在集合失败获取。
但它是一个有点哈克...是否有任何优雅的解决这个问题呢?

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总是会产生这种形式的结果,是有可能在默认情况下重写我的所有车型和收藏解析功能? (对不起,我是一个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...)

推荐答案

如果您收到的数据是由结果包裹成员,并作出相应的反应,您可以测试。例如,

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/

如果你想概括这种行为,无论是从这个基类派生你所有的模型类或修改骨干的原型来提供这个功能:

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天全站免登陆