Skip to content

Instantly share code, notes, and snippets.

@chrislewis
Created September 21, 2009 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrislewis/190442 to your computer and use it in GitHub Desktop.
Save chrislewis/190442 to your computer and use it in GitHub Desktop.
/*
* An object for building an object of handler methods, which can
* can then be added via Element.addMethods. Example result:
*
* {
* mousedown: function(element, fn) {
* element = $(element);
* element.observe("mousedown", fn.bind(element));
* return element;
* },
* mouseup: function(element, fn) {
* element = $(element);
* element.observe("mouseup", fn.bind(element));
* return element;
* }
* }
*/
var InlineHandlers = {
events: "mousedown,mouseup".split(","),
build: function() {
var methods = {};
this.events.map(function(evt) {
methods[evt] = this._buildObservationMethod(evt);
}.bind(this));
return methods;
},
_buildObservationMethod: function(name) {
return function(element, fn) {
element = $(element);
element.observe(name, fn.bind(element));
return element;
};
}
};
Element.addMethods(InlineHandlers.build());
// Now we have jQuery-style event registration.
$("aLink")
.mousedown(function(e) {
this.update("DOWN - " + e);
})
.mouseup(function(e) {
this.update("UP - " + e);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment