Javascript 在集合上设置属性 - 骨干 js

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5930656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 19:33:28  来源:igfitidea点击:

Setting attributes on a collection - backbone js

javascriptcollectionsbackbone.js

提问by idbentley

Collections in backbone js don't allow you to setattributes, but I often find that there is need to store some meta-information about a collection. Where is the best place to set that information?

骨干 js 中的集合不允许你设置set属性,但我经常发现需要存储一些关于集合的元信息。设置该信息的最佳位置在哪里?

采纳答案by Wytze

It's probably best to use Collection in exactly the way it was intended: as a bundle of models. (Julien already commented this on the OP, I'd like to give an explanation why I think he is right)

最好完全按照预期的方式使用 Collection:作为一组模型。(Julien 已经在 OP 上对此发表了评论,我想解释一下为什么我认为他是对的)

Let's say you are thinking of a Library (collection) of Book (model) as in Backbone's documentation examples. It makes sense that you've got meta information about the library which you want to store, like the address where this book library is located.

假设您正在考虑 Backbone 的文档示例中的图书(模型)的图书馆(集合)。您已经获得了有关要存储的图书馆的元信息,例如该图书图书馆所在的地址,这是有道理的。

The trick is not to think of it as meta information. You've got a library that has a lot of properties, and one of those properties is its collection of books.

诀窍是不要将其视为元信息。你有一个拥有很多属性的图书馆,其中一个属性就是它的藏书。

var Book = Backbone.Model.extend({ 
    title: "Moby Dick"
});

var Collection = Backbone.Collection.extend({
    model: Book
});

var Library = {
    address: '45th Street',
    collection: Collection
};

In this example I've defined Library as a plain JavaScript object. Obviously you can also make Library be a model, so that it has all the Backbone bells and whistles. My point here is that you need to represent reality in a more realistic way by taking one step back and seeing that extra properties that you want to assign to the Collection are in fact sibling properties of an object one level up: the Library in this case.

在这个例子中,我将 Library 定义为一个普通的 JavaScript 对象。显然,您也可以让 Library 成为一个模型,以便它拥有所有 Backbone 花里胡哨的功能。我的观点是,您需要通过退后一步,看到您想要分配给 Collection 的额外属性实际上是上一级对象的同级属性:在这种情况下为 Library,从而以更现实的方式表示现实.



回答by Raynos

Just .extendthe collection with a meta data storage function.

只是.extend具有元数据存储功能的集合。

var MyCollection = Backbone.Collection.extend({
    initialize: function() {
        ...

        this._meta = {};
    },
    model: ...
    meta: function(prop, value) {
        if (value === undefined) {
            return this._meta[prop]
        } else {
            this._meta[prop] = value;
        }
    },
});

var collection = new MyCollection();
collection.add(someModels);
collection.meta("someProperty", value);

...

var value = collection.meta("someProperty");

There may be better places for storing specificmeta data but this depends completely on what the meta data is.

可能有更好的地方来存储特定的元数据,但这完全取决于元数据是什么。

For storing generic meta data extending your collection constructor with a method to do deal with that should work.

用于存储通用元数据,扩展您的集合构造函数,并使用一种方法来处理它应该工作。

Be wary that if this meta data needs to be stored and loaded from the server then you've got a bigger task at hand.

请注意,如果需要从服务器存储和加载此元数据,那么您手头的任务更大。

回答by Antipin

I've upgrated Raynos's approach with event triggering, so we can bind to collection's attributes update.

我已经通过事件触发升级了 Raynos 的方法,因此我们可以绑定到集合的属性更新。

cls.groups = Backbone.Collection.extend({

    // ...

    // Reference to this collection's model.
    model: cls.group,

    initialize: function() {
        this._attributes = {}
    },

    // Extend collection with ability to store attributes and trigger events on attributes changing
    attr: function(prop, value) {
        if (value === undefined) {
            return this._attributes[prop]
        } else {
            this._attributes[prop] = value;
            this.trigger('change:' + prop, value);
        }
    },

    // ...

});


cls.group = Backbone.View.extend({

    // ...

    initialize: function() {

        // Catching attribute update
        app.groups.on('change:selected', function(value) {
            // ...
        }, this);
    },

    // ...

    events: {
        'click' : function(e) {
            // Set collection meta attribute on model's view click event
            app.groups.attr('selected', this.model.cid);
        }
    }

    // ...

});

回答by yves amsellem

Using the function metaof @Raynos solution with only one parameter do not worked for me. So I've used the following code:

meta仅使用一个参数的@Raynos 解决方案的功能对我不起作用。所以我使用了以下代码:

var MyCollection = Backbone.Collection.extend({
    initialize: function() {
        this._meta = {};
    },
    put: function(prop, value) {
        this._meta[prop] = value;
    },
    get: function(prop) {
        return this._meta[prop];
    }
});

var collection = new MyCollection();
collection.put("someProperty", 12);
alert(collection.get("someProperty"));

Hope it'll helps.

希望它会有所帮助。