坚持&在backbone.js 集合中加载元数据 [英] Persisting & loading metadata in a backbone.js collection

查看:13
本文介绍了坚持&在backbone.js 集合中加载元数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用backbone.js 的情况,我有一个模型集合,以及一些关于模型的附加信息.例如,假设我正在返回一个数量列表:它们有一个与每个模型相关联的数量.现在假设每个数量的单位总是相同的:比如说夸脱.然后我从我的服务中返回的 json 对象可能是这样的:

I have a situation using backbone.js where I have a collection of models, and some additional information about the models. For example, imagine that I'm returning a list of amounts: they have a quantity associated with each model. Assume now that the unit for each of the amounts is always the same: say quarts. Then the json object I get back from my service might be something like:

{
    dataPoints: [
         {quantity: 5 },
         {quantity: 10 },
         ...
    ],
    unit : quarts
 }

现在主干集合没有真正的机制将这个元数据与集合相关联,但在这个问题中向我建议:在集合上设置属性 - 骨干 js,我可以使用 .meta(property, [value]) 样式扩展集合功能 - 这是一个很好的解决方案.然而,很自然地,我们希望能够像上面那样从 json 响应中干净地检索这些数据.

Now backbone collections have no real mechanism for natively associating this meta-data with the collection, but it was suggested to me in this question: Setting attributes on a collection - backbone js that I can extend the collection with a .meta(property, [value]) style function - which is a great solution. However, naturally it follows that we'd like to be able to cleanly retrieve this data from a json response like the one we have above.

Backbone.js 为我们提供了 parse(response) 函数,它允许我们结合 url 属性指定从何处提取集合的模型列表.然而,我不知道如何在不重载 fetch() 的情况下创建更智能的函数,这会删除已经可用的部分功能.

Backbone.js gives us the parse(response) function, which allows us to specify where to extract the collection's list of models from in combination with the url attribute. There is no way that I'm aware of, however, to make a more intelligent function without overloading fetch() which would remove the partial functionality that is already available.

我的问题是:有没有比重载 fetch()(并尝试调用它的超类实现)更好的选择来实现我想要实现的目标?

My question is this: is there a better option than overloading fetch() (and trying it to call it's superclass implementation) to achieve what I want to achieve?

谢谢

推荐答案

就个人而言,我会将 Collection 包装在另一个 Model 中,然后覆盖 parse,像这样:

Personally, I would wrap the Collection inside another Model, and then override parse, like so:

var DataPointsCollection = Backbone.Collection.extend({ /* etc etc */ });
var CollectionContainer = Backbone.Model.extend({
    defaults: {
        dataPoints: new DataPointsCollection(),
        unit: "quarts"
    },
    parse: function(obj) {
        // update the inner collection
        this.get("dataPoints").refresh(obj.dataPoints);

        // this mightn't be necessary
        delete obj.dataPoints;

        return obj;
    }
});

Collection.refresh() 调用使用新值更新模型.按照之前的建议将自定义 meta 值传递给 Collection 可能会阻止您绑定到这些元值.

The Collection.refresh() call updates the model with new values. Passing in a custom meta value to the Collection as previously suggested might stop you from being able to bind to those meta values.

这篇关于坚持&在backbone.js 集合中加载元数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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