I have this function in my view:
saveData: function(){
this.collection.setSelected(this.collection.where({name : this.$('#projects').val()}));
}
And the corresponding function in my collection:
setSelected: function(project){
this.each(function(model){
model.set({selected: false});
});
project[0].set({selected: true});
}
So, my question is why am I required to access the first element of the Project array to access the actual Model? Am I doing something wrong?
Also, it seems a little OTT to loop round all the models in the collection setting to false to then set one to true, is this the correct way to do things?
Because you’re using
where, which returns an array. You could usefindin this case, which returns the first matching object.If you only want one model selected at a time, then it might be better to find the selected one and deselect it. Something like:
And: