I was adding some much needed events to my GoodCarousel plugin for MooTools recently. I added what I thought others might need,
startUp, beforeSlide, afterSlide, end, startOver, mouseEnter, mouseLeave
But since I’m lazy, I didn’t want to explicitly write a function for each of those events and then put them in options argument. I’m just too lazy. So here’s what I did instead.
gc = new GoodCarousel("good-carousel", {width: 495, height: 300, loops: 1});
var events = ["startUp", "beforeSlide", "afterSlide", "end", "startOver", "mouseEnter", "mouseLeave"];
var fn = function(event) {
console.log(event);
};
events.each(function(evt, i){
gc.addEvent(evt, fn.pass(evt));
});
I made an array of event names and a function that simply accepts an argument and logs the argument to the console. The next part is where the events are actually added. In the each, gc.addEvent is used to add each event. The fn.pass(evt) allows me to pass the event name to the function I’m calling so that it can be logged.