Skip to content

Instantly share code, notes, and snippets.

@chrislewis
Created September 8, 2009 19:40
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/183168 to your computer and use it in GitHub Desktop.
Save chrislewis/183168 to your computer and use it in GitHub Desktop.
var EventAttacher = Class.create({
events: "click,focus".split(","),
initialize: function(element, handler) {
this.element = $(element);
this.handler = handler;
this.events.each(this._buildObservationMethod.bind(this));
},
_buildObservationMethod: function(base) {
this[this._buildMethodName(base)] = function() {
this.element.observe(base, this.handler);
return this;
}.bind(this);
},
_buildMethodName: function(base) {
return "is" + base.capitalize() + "ed";
}
});
Function.prototype.callWhen = function(element) {
return new EventAttacher(element, this);
}
var f = function(e) {
alert(e);
}.callWhen("button").isClicked().isFocused();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment