坚持和放大器;在Backbone.js的集合装载的元数据 [英] Persisting & loading metadata in a backbone.js collection

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

问题描述

我在使用Backbone.js的,我有模型的集合的情况,以及有关车型的一些其他信息。例如,假设我回金额的清单:他们每个型号数量。现在假定每个数量的单位始终是相同的:说夸脱。那么JSON对象我从我的服务回报可能是这样的:

  {
    数据点:
         {数量:5},
         {数量:10},
         ...
    ]
    单位:夸脱
 }

现在骨干藏品都没有真正的机制,本身这个元数据与采集相关联,但有人建议我在这个问题:<一href=\"http://stackoverflow.com/questions/5930656/setting-attributes-on-a-collection-backbone-js\">Setting属性对集合 - 骨干JS ,我可以用延长集合.META(财产,[值])风格的功能 - 这是一个很好的解决方案。然而,自然接下去我们希望能够从干净像一个我们上面有一个JSON响应检索此数据。

Backbone.js的为我们提供了解析(响应)功能,它允许我们指定从哪里结合的<$ C $提取模式集合的列表C>网址属性。没有办法,我所知道的,但是,做出更智能化的功能,无需重载取()这将删除部分功能已经可用。

我的问题是:有没有比超载更好的选择取()(并试图调用它的超类实现)实现我想达到什么? / p>

感谢


解决方案

就个人而言,我会换行收藏在另一个模式,然后覆盖解析,就像这样:

  VAR DataPointsCollection = Backbone.Collection.extend({/ *等等等等* /});
VAR CollectionContainer = Backbone.Model.extend({
    默认值:{
        数据点:新DataPointsCollection()
        单位:夸脱
    },
    解析:函数(OBJ){
        //更新内部集合
        。this.get(数据点)刷新(obj.dataPoints);        //这可能不是必要
        删除obj.dataPoints;        返回OBJ;
    }
});

Col​​lection.refresh()呼叫更新使用新值模型。传递一个自定义的收藏价值为previously建议可能会从能够绑定到这些元值阻止你。

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
 }

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

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?

Thanks

解决方案

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

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