I have the following collection:
UsersMod.users_list = Backbone.Collection.extend({
add : function(model) {
var duplicate = this.any(function(_model) {
return _model.get('user') === model.get('user');
});
if (duplicate) {
return false;
}
Backbone.Collection.prototype.add.call(this, model);
}
});
This currently checks for a duplicate model in the collection and returns false if one is found. What I'm trying to do is when a duplicate is found, remove the old one and replace it with the new one.
Any help is much appreciated, thanks in advance :)
Try this:
UsersMod.users_list = Backbone.Collection.extend({
add : function(model) {
var duplicates = this.filter(function(_model) {
return _model.get('user') === model.get('user');
});
if (! _(duplicates).isEmpty) {
this.remove(duplicates);
}
Backbone.Collection.prototype.add.call(this, model);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With