Setting attributes on a collection - backbone js

前端 未结 4 1261
忘了有多久
忘了有多久 2020-12-02 13:17

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

4条回答
  •  醉酒成梦
    2020-12-02 13:21

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

    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.

提交回复
热议问题