My backbone model's idAttribute
can be anything (Backbone's default is "id").
I would like to check before inserting the model in a Backbone collection if that collection already contains a model with the same idAttribute value.
myCollection.contains(myNewModel)
is always returning false
. I guess that's because the candidate model is not necessarily the same instance that is already in the collection.
I tried
idAttribute: string = myColllection.model.prototype.idAttribute; // Works fine
let id: string = itemToInsert.get(idAttribute); // Works fine
let exists : boolean : myCollection.findWhere({ idAttribute : id }); // Undefined!
I would like something like
let test: boolean = _.any(self.collection, item => idAttributeValue === item.get("idAttribute")).value();
but I'm not sure the precise syntax.