Sunday, March 11, 2012

Passing arguments to handlers

Not as far as I know. What you can do, though, is have the function you pass as the handler be created in teh context of an object that maybe has state; you can then refer to that state. For example:

someControl = function(element){
someControl.initializeBase(this,[element]);
this._privateValue = 'Hi there';
}

someControl.prototype = {
initialize : function(){someControl.callBaseMethod(this,'initialize'),
dispose : functio(){someControl.callBaseMethod(this,'dispose'),
handleClick : function(ev){
alert(this._privateValue);
}
}

Type.registerClass('someControl',Sys.UI.Control)

// later...

var ctrl = new someControl(element);
$addHandler(btn,'click',Function.createDelegate(ctrl,ctrl.handleClick));

I haven't tried it, but you might even be able to do all that w/o making a formal 'class' since classes are just special functions... you might be able to get away with just attaching the value and the function handler to some random js object.

No comments:

Post a Comment