我如何能坚持一个集合自定义属性取 [英] How can I persist custom attributes over a collection fetch

查看:86
本文介绍了我如何能坚持一个集合自定义属性取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个资产骨干已调用自定义属性中选择模式。其在这个意义上,它不是在服务器侧的对象的一部分定义。我用它来重新的资产列表的用户当前选择present。

I have a an "Asset" backbone model that has a custom attribute called "selected". Its custom in the sense that it is not part of the object on the server side. I use to represent which of the list of assets the user has currently selected.

var Asset = Backbone.Model.extend({
    defaults: {
        selected: false
    },

    idAttribute: "AssetId"
});

这模式是骨干集,我取定期获得来自服务器的任何变化的一部分。

This model is part of a backbone collection that I fetch periodically to get any changes from the server.

我的问题是,每次我取集合时间,集合做一个复位(我可以通过侦听复位事件告知),因此所选择的属性的值由传来的数据全军覆没从Ajax请求。

The problem I have is that every time I fetch the collection, the collection is doing a reset (I can tell by the listening for the reset event) and hence the value of the selected attribute is wiped out by the data coming in from the ajax request.

的Backbone.js的文档似乎表明存在将解决这个问题的一个智能合并。我相信,我在做这在我的获取方法

The backbone.js documentation seems to suggest that there is a intelligent merge that will solve this problem. I believe I'm doing this in my fetch methods

 allAssets.fetch({  update: true ,cache: false});

和我还设置了idAttribute字段中的模式,使进来的对象的ID可与集合中的对象进行比较。

And I have also set the "idAttribute" field in the model so that the ids of the object coming in can be compared with the objects in the collection.

我已经解决了这个问题的方法是通过我的收藏对象,写我自己的Parse方法

The way I have solved this is by writing my own Parse method in my collection object

 parse: function (response) {
    // ensure that the value of the "selected" for any of the models
    // is persisted into the model in the new collection
    this.each(function(ass) {
        if (ass.get("selected")) {
            var newSelectedAsset = _.find(response, function(num) { return num.AssetId == ass.get("AssetId"); });
            newSelectedAsset.selected = true;
        }
    });
    return response;
}

有没有更好的方法来做到这一点?

Is there a better way to do this?

推荐答案

Col​​lection.update (介绍在骨干0.9.9)确实尝试合并现有的车型,但通过合并在新的模型中的所有属性集到旧模式确实如此。如果检查骨干源$ C ​​$ C ,你会看到

Collection.update (introduced in Backbone 0.9.9) does indeed try to merge existing models, but does so by merging all set attributes in the new model into the old model. If you check Backbone source code, you'll see

if (existing || this._byCid[model.cid]) {
    if (options && options.merge && existing) {
        existing.set(model.attributes, options);
        needsSort = sort;
    }
    models.splice(i, 1);
    continue;
}

所有的属性,包括默认值,设置,这就是为什么你选择的属性重新设置为false。删除默认值选择将工作打算:比较 http://jsfiddle.net/nikoshr/s5ZXN/ http://jsfiddle.net/nikoshr/s5ZXN/3/

这是说,我不会依赖一个模型属性来存储我的应用程序的状态,我宁愿将其移动到一个控制器在其他地方。

That said, I wouldn't rely on a model property to store my app state, I would rather move it to a controller somewhere else.

这篇关于我如何能坚持一个集合自定义属性取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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