On 07/09/2007, at 18:35, Michael Geary wrote:
Use a closure.
Or bring your own `bindAsEventListener` into play.
Something like
Function.prototype.bindAsEventListener = function(o) {
var _m = this;
var args = [].slice.call(arguments, 0);
var obj = args.shift();
return function(e) {
return _m.apply(obj, [e, this].concat(args));
}
};
function SomeClass() {
// constructor code
}
SomeClass.prototype.clickHandler = function(e, el) {
// `e` points to the Event object
// `el` points to the element which the handler is assigned to
// `this` points to whatever we set it to (an instance of SomeClass,
for example)
};
var myObject = new SomeClass();
$('selector').bind('click', myObject.clickHandler.bindAsEventListener
(myObject));
From: [EMAIL PROTECTED]
I'm not sure if I've completely missed this in the docs, but
I'm wondering if there is an easier way of achieving the following:
$(".button").bind("click", {that:this}, function(event) {
var that = event.data.that;
that.handleEvent();
return false;
});
I could do the same thing in prototype using "bindAsEventListener"
like this:
Event.observe($$(".button"), 'click',
this.handleEvent.bindAsEventListener(this));
This would help tidy everything up a lot, since I'd like to
maintain a class-based pattern where "this" is used a lot.
Passing it in as event data seems like overkill.
Thanks :)
--
Choan Gálvez
<[EMAIL PROTECTED]>
<http://choangalvez.nom.es/>