forked from millermedeiros/js-signals
-
Notifications
You must be signed in to change notification settings - Fork 1
Examples
millermedeiros edited this page Feb 27, 2012
·
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(), //past tense is the recommended signal naming convention
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
var i = 0;
myObject.started.add(function(){
i += 1;
alert(i);
});
myObject.started.dispatch(); //will alert 1
myObject.started.dispatch(); //will alert 2
var i = 0;
myObject.started.addOnce(function(){
i += 1;
alert(i);
});
myObject.started.dispatch(); //will alert 1
myObject.started.dispatch(); //nothing happens
var i = 0;
myObject.started.add(function(){
i += 1;
alert(i);
});
myObject.started.dispatch(); //will alert 1
myObject.started.active = false;
myObject.started.dispatch(); //nothing happens
myObject.started.active = true;
myObject.started.dispatch(); //will alert 2
myObject.started.add(function(){
myObject.started.halt(); //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();
var foo = 'bar';
var obj = {
foo : 10
};
function handler1(){
alert(this.foo);
}
function handler2(){
alert(this.foo);
}
//note that you cannot add the same handler twice to the same signal without removing it first
myObject.started.add(handler1); //default execution context
myObject.started.add(handler2, obj); //set a different execution context
myObject.started.dispatch(); //first handler will alert "bar", second will alert "10".
var handler1 = function(){
alert('foo');
};
var handler2 = function(){
alert('bar');
};
myObject.started.add(handler1); //default priority is 0
myObject.started.add(handler2, null, 2); //setting priority to 2 will make `handler2` execute before `handler1`
myObject.started.dispatch(); //will alert "bar" than "foo"
var handler1 = function(){
alert('foo bar');
};
var handler2 = function(){
alert('lorem ipsum');
};
var binding1 = myObject.started.add(handler1); //methods `add()` and `addOnce()` returns a SignalBinding object
myObject.started.add(handler2);
myObject.started.dispatch(); //will alert "foo bar" than "lorem ipsum"
binding1.active = false; //disable a single binding
myObject.started.dispatch(); //will alert "lorem ipsum"
binding1.active = true;
myObject.started.dispatch(); //will alert "foo bar" than "lorem ipsum"
var handler = function(){
alert('foo bar');
};
var binding = myObject.started.add(handler); //methods `add()` and `addOnce()` returns a SignalBinding object
binding.execute(); //will alert "foo bar"
var binding = myObject.started.add(function(){
alert('foo bar');
});
var handler = binding.getListener(); //reference to the anonymous function
var binding = myObject.started.add(function(){
alert('foo bar');
});
myObject.started.dispatch(); //will alert "foo bar"
binding.detach();
alert(binding.isBound()); //will alert `false`
myObject.started.dispatch(); //nothing happens
var binding1 = myObject.started.add(function(){
alert('foo bar');
});
var binding2 = myObject.started.addOnce(function(){
alert('foo bar');
});
alert(binding1.isOnce()); //alert "false"
alert(binding2.isOnce()); //alert "true"
var foo = 'bar';
var obj = {
foo : "it's over 9000!"
};
var binding = myObject.started.add(function(){
alert(this.foo);
});
myObject.started.dispatch(); //will alert "bar"
binding.context = obj;
myObject.started.dispatch(); //will alert "it's over 9000!"
var binding = myObject.started.add(function(a, b, c){
alert(a +' '+ b +' '+ c);
});
binding.params = ['lorem', 'ipsum']; //set default parameters of the binding
myObject.started.dispatch('dolor'); //will alert "lorem ipsum dolor"
function onStart(a){
console.log(a);
}
myObject.started.add(onStart);
myObject.started.has(onStart); // true
myObject.started.memorize = true; // default is false
myObject.started.dispatch('foo');
// add()/addOnce() will automatically fires listener if signal was dispatched before
// will log "foo" since it keep record of previously dispatched values
myObject.started.addOnce(console.log, console);
myObject.started.forget(); // forget previously dispatched values (reset signal state)
myObject.started.addOnce(console.log, console); // won't log till next dispatch (since it "forgot")
myObject.started.dispatch('bar'); // log "bar"
Check the CompoundSignal repository.
In some cases it might be useful to listen/dispatch to arbitrary event types, see SignalEmitter for more info.
For more examples check the unit tests located inside the dev/tests folder.
Check the documentation for a a full reference of available methods and properties.