A Compilation of YUI Callback

I have been coding JavaScript with YUI for quite a while now. Still, I am consistently confused by various callback functions in YUI. The thing that troubled me the most was the parameters those callback functions take. The API documentation does not provide any information about this. For example, if you go to YAHOO.util.Event, under addListener method, the description for parameter fn as follow: <Function> The method the event invokes.

Yes, it's a straightforward description, but it doesn't help too much. What I really need is the signature of this callback function. For the purpose of my own use, and to benefit the community, I decided to make a compilation of callbacks in YUI.

Callback for Native Browser Events

0

1

2

3

4

5

6

varcallback=function(e,o){

alert(o.msg);

};

YAHOO.util.Event.addListener(elemId,"click",callback,{msg:"hi"});

where e - Name of the event fired.o - Object that passed as a parameter to the callback function.

Callback for Custom Events

Depending on how the custom event is fired, the parameters that the callback function receives varies.

If the custom event is fired with parameter signatureYAHOO.util.CustomEvent.LIST, which is by default, the parameters the callback function receives will be:

0

1

2

3

4

5

6

7

8

9

10

11

12

varcallback=function(e,a,o){

//a is an array contains arguments fire method: arg1, arg2, ...

//o is the object passed from the subscribe method

alert(o.msg);

};

//to fire this event

eventObj.fire(arg1,arg2,...);

//to listen to this event

eventObj.subscribe(callback,{msg:"hi"});

wheree - Name of this custom event.a - Arguments that passed to the fire method of this event.o - Object that passed as a parameter to this callback function.

If the custom event is fired with parameter signatureYAHOO.util.CustomEvent.FLAT, the callback function receives two arguments:

0

1

2

3

4

5

6

7

8

9

10

11

12

varcallback=function(a,o){

//a is the first argument that passed to fire method of this event

//o is the object passed from the subscribe method

alert(o.msg);

};

//to fire this event

eventObj.fire(arg);

//to listen to this event

eventObj.subscribe(callback,{msg:"hi"});

wherea - First argument that passed to fire method of this evento - Object that passed as a parameter to the callback function

Callback for Connection Manager

0

1

2

3

4

5

6

7

varcallback=

{

success:function(o){//do something},

failure:function(o){//do something},

argument:{name:"YUI"}

};

This is not a callback function, rather it's a callback object that contains two callback functions. You can pass additional parameters to each callback function through argument field of this object. To access this filed inside of the callback function, use o.argument.

Above are the callbacks I can think of so far. If you have any suggestion, feel free to post it in the comment section.