forked from millermedeiros/js-signals
-
Notifications
You must be signed in to change notification settings - Fork 1
Examples
millermedeiros edited this page Nov 27, 2010
·
18 revisions
Few examples on how to use Signals for custom event dispatching, check the documentation for a a full reference of available methods and properties.
//store local reference for brevity
var Signal = signals.Signal;
//custom object that dispatch signals
var myObject = {
started : new Signal(),
stopped : new Signal()
};
function onStarted(param1, param2){
alert(param1 + param2);
}
myObject.started.add(onStarted); //add listener
myObject.started.dispatch('foo', 'bar'); //dispatch signal passing custom parameters
myObject.started.remove(onStarted); //remove a single listener
function onStopped(){
alert('stopped');
}
function onStopped2(){
alert('stopped listener 2');
}
myObject.stopped.add(onStopped);
myObject.stopped.add(onStopped2);
myObject.stopped.dispatch();
myObject.stopped.removeAll(); //remove all listeners of the `stopped` signal
myObject.started.add(function(){
myObject.started.stopPropagation(); //prevent next listeners on the queue from being executed
});
myObject.started.add(function(){
alert('second listener'); //won't be called since first listener stops propagation
});
myObject.started.dispatch();
myObject.started.add(function(){
return false; //if handler returns `false` will also stop propagation
});
myObject.started.add(function(){
alert('second listener'); //won't be called since first listener stops propagation
});
myObject.started.dispatch();