Alexander Mills
Alexander Mills

Reputation: 100496

Update model function on Backbone collection

I created this function on the superclass for all my Backbone collections:

updateModel: function (_id, updateInfo) {
    for (var i = 0; i < this.models.length; i++) {
        var model = this.models[i];
        if (String(model.get('_id')) == String(_id)) {
            model.set(updateInfo);
            break;
        }
    }
},

the purpose of the function is to simply update a model given the server-defined _id property.

Is the function I wrote useful? or should I simply use this instead:

collection.add(model, {merge: true});

Upvotes: 0

Views: 99

Answers (1)

yumianyang
yumianyang

Reputation: 21

try this:

updateModel: function (_id, updateInfo) {
    this.some(function (oneModelInfo) {
        if (_id == oneModelInfo._id) {
            oneModelInfo.set(updateInfo);
            return true;
        }
    });
},

Upvotes: 1

Related Questions