You are returning true from the function you passed into _.each
. That is different from your contains function. Here is a way to get it working:
var Cart = Backbone.Collection.extend({
contains : function(aProduct){
var doesContain = false;
_.each(this.models, function( element, index, list ){
if( element.get('product').get('id') == aProduct.get('id') ){
doesContain = true;
}
});
return doesContain;
}
});
However, an even better way (assuming that is lodash or underscore), would be to use _.any
var Cart = Backbone.Collection.extend({
contains : function(aProduct){
return _.any(this.models, function( element ){
if( element.get('product').get('id') == aProduct.get('id') ){
return true;
}
});
}
});