yui – custom event

Note to self on how to setup custom even.
Step 1: Define a custom event

var onCustomEvent1 = new YAHOO.util.CustomEvent('onCustomEvent');

Step 2: Make methods subscribe to the event

onCustomEvent1.subscribe(method1);
onCustomEvent1.subscribe(method2);

etc.You can also pass arguments to the subscribe method, which can be accessed by the subscribed method, as explained below.

Step 3: Whenever that event is supposed to be triggerd, cause the event to be executed

onCustomEvent1.fire();

You can also pass arguments to the fire method, these arguments can then be accessed in the subscribe method explained below.

onCustomEvent1.fire({action:'fire'});
function method1(event, arguments, obj) {}
  • event returns the name of the custom event
  • arguments is the set of arguments that are passed in the fire event
  • obj is the argument that is passed in the subscribe method.

Calling the fire() method causes all the listener methods to get fired. Simple stuff, amazing uses as well, maybe its just me but i am sure i wont remember this the second time i have look at it.

About rp

Architect for large, highly scalable LAMP applications and Technical Manager with special focus on metrics based continuous improvement of teams and products. Rajat has close to a decade of experience of a very wide range of skills related to infrastructure, middleware, app servers all the way to front-end technologies and software development methodologies including agile, iterative waterfall, waterfall as well as ah-hoc startup using the right approach in the right context to reduce time to market.