Select - deselect list


#1

I am currently having a list of items. The user is able to select an item, the item is pushed into an collection and added to the array. How would you go about to make a second click on the same item remove the item from the collection?

//client side
"click .item": function(event, template){
    Meteor.call('updateItem', Session.get('category'), this._id);
    template.$(event.currentTarget).css('background-color', '#f9f9f9');
}

//server method
var currentItem = Items.findOne({category: name});
var check = Items.find({category: name, items: item}).count();
if(check === 0){
 Items.update({ _id: category._id},{$push: { items: item}});
}

This is the method called when first clicking the item - i now want to do an Items.remove the next time i press the same item to simulate a select/deselect functionallity.


#2

Your code is to short but if

if(check === 0){
 Items.update({ _id: category._id},{$push: { items: item}});
}

works, then you could try to

if(check === 0){
 Items.update({ _id: category._id},{$push: { items: item}});
} else {
 Items.update({ _id: category._id},{$pull: { items: item}});
}