A simple favourites system. I wrote it like this:
$(".favourite").click(function(){
// It's active; deactivate it
if ($(this).hasClass('active')) {
changeFavourites('remove', -1);
}
// It's not active; activate it
else {
changeFavourites('add', 1);
}
});
However that was some old code and I'm refactoring it. Would this code be better?
// Handle clicking on favourites to add or remove them
// Cannot use .click() ( http://stackoverflow.com/a/12673849/938236 )
$(document).on('click', ".favourite.active", function(){
changeFavourites('remove', -1);
});
// :not() http://stackoverflow.com/a/520271/938236
$(document).on('click', ".favourite:not(.active)", function(){
changeFavourites('add', 1);
});
The main issue I see is that I'm embedding a conditional within the selector instead of making it an obvious if. I'm concerned the readability of the code will decrease and the future me will not be happy when I come back to this code. If I'm not mistaken, I think the second code could be called polymorphism.