Implementing jQuery-Like Event Syntax in MooTools

By  on  

If you take a peek inside the MooTools core Element/Events source code, you'll see the following collection of events:

Element.NativeEvents = {
	click: 2, dblclick: 2, mouseup: 2, mousedown: 2, contextmenu: 2, //mouse buttons
	mousewheel: 2, DOMMouseScroll: 2, //mouse wheel
	mouseover: 2, mouseout: 2, mousemove: 2, selectstart: 2, selectend: 2, //mouse movement
	keydown: 2, keypress: 2, keyup: 2, //keyboard
	focus: 2, blur: 2, change: 2, reset: 2, select: 2, submit: 2, //form elements
	load: 1, unload: 1, beforeunload: 2, resize: 1, move: 1, DOMContentLoaded: 1, readystatechange: 1, //window
	error: 1, abort: 1, scroll: 1 //misc
};

As you probably know, in order to add an event, you need to code something similar to:

	$('element').addEvent('click',function(e) {
		//do stuff
	});

In jQuery you code something like:

	$('#element').click(function(e) {
		//do stuff
	});

I don't prefer that syntax but if you're a Moo developer and you wanted to implement jQuery-like syntax for all events or you're slowly making your way from jQuery to MooTools, you can use the event collection cited above and the snippet to follow to quickly make that possible.

//hash the element.natives so we can do stuff with it
var hash = new Hash(Element.NativeEvents);
//remove items that need to be replaced, add their replacements
hash.erase('mouseover').erase('mouseout').erase('DOMMouseScroll');
hash.include('mouseenter',1).include('mouseleave',1);
//initialize this
var eventHash = new Hash({});
//for every event type, add to our hash
hash.getKeys().each(function(event){
	eventHash[event] = function(fn) {
		this.addEvent(event,fn);
		return this;
	}
});
//make it happen
Element.implement(eventHash);

Here's a few examples of using the newly implemented methods:

/* examples */
window.addEvent('domready',function() {
	$$('a').click(function(e) {
		e.stop();
		alert('mouse click');	
	});
	$$('a').contextmenu(function(e) {
		e.stop();
		alert('right click');	
	});
	$('myInput').keypress(function(e) {
		alert('key pressed: ' + e.key);
	});
});

Like I said, I don't find this syntax appealing, but I wanted to show how easy it is to do.

Recent Features

  • By
    An Interview with Eric Meyer

    Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...

  • By
    CSS Filters

    CSS filter support recently landed within WebKit nightlies. CSS filters provide a method for modifying the rendering of a basic DOM element, image, or video. CSS filters allow for blurring, warping, and modifying the color intensity of elements. Let's have...

Incredible Demos

  • By
    Create Keyboard Shortcuts with Mousetrap

    Some of the finest parts of web apps are hidden in the little things.  These "small details" can often add up to big, big gains.  One of those small gains can be found in keyboard shortcuts.  Awesome web apps like Gmail and GitHub use loads of...

  • By
    9 Mind-Blowing WebGL Demos

    As much as developers now loathe Flash, we're still playing a bit of catch up to natively duplicate the animation capabilities that Adobe's old technology provided us.  Of course we have canvas, an awesome technology, one which I highlighted 9 mind-blowing demos.  Another technology available...

Discussion

  1. I think that the sintax clear and user friendly of mootools is one of the best things for ppl in doubt between JQuery and MooTools. I liked a lot the article you posted about a page with both the two framework… but this way of write event in a jquery way in mootools is something that who uses mootools will not use, and who uses jquery can’t see how easy is write and speak in MooTools syntax… You and your blog you are a reference point for many of us, is not a criticism.
    see you
    Moonkiki

  2. I don’t like jQuery’s event syntax at all. In fact, I think it’s ugly. But someone trying to move from jQuery to MooTools could benefit from this.

  3. yes, you’re right. Little step from ugly to pure code.

    Yesterday i read in some links from your twitter to other internet’s beach, the Einstein phrase: IF YOU CAN’T EXPLAIN IT SIMPLY, YOU DON’T UNDERSTAND IT ENOUGH…. i thougth suddenly to MooTools and Jquery syntax :)

  4. Nice little jQuery to Mootools trick!

  5. @david: great job… but this behavior couldn’t be used only with the “migrators”, but this could at least clear the views a little bit :)

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!