RSVP.EventTarget Class bower_components/rsvp/lib/rsvp/events.js:19


Show:

Methods

Show:

mixin

(object) private

RSVP.EventTarget.mixin extends an object with EventTarget methods. For Example:

1
2
3
4
5
6
7
8
9
var object = {};

RSVP.EventTarget.mixin(object);

object.on('finished', function(event) {
  // handle event
});

object.trigger('finished', { detail: value });

EventTarget.mixin also works with prototypes:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var Person = function() {};
RSVP.EventTarget.mixin(Person.prototype);

var yehuda = new Person();
var tom = new Person();

yehuda.on('poke', function(event) {
  console.log('Yehuda says OW');
});

tom.on('poke', function(event) {
  console.log('Tom says OW');
});

yehuda.trigger('poke');
tom.trigger('poke');

Parameters:

object Object
object to extend with EventTarget methods

off

(eventName, callback) private

You can use off to stop firing a particular callback for an event:

1
2
3
4
5
6
7
8
function doStuff() { // do stuff! }
object.on('stuff', doStuff);

object.trigger('stuff'); // doStuff will be called

// Unregister ONLY the doStuff callback
object.off('stuff', doStuff);
object.trigger('stuff'); // doStuff will NOT be called

If you don't pass a callback argument to off, ALL callbacks for the event will not be executed when the event fires. For example:

1
2
3
4
5
6
7
8
9
10
var callback1 = function(){};
var callback2 = function(){};

object.on('stuff', callback1);
object.on('stuff', callback2);

object.trigger('stuff'); // callback1 and callback2 will be executed.

object.off('stuff');
object.trigger('stuff'); // callback1 and callback2 will not be executed!

Parameters:

eventName String
event to stop listening to
callback Function
optional argument. If given, only the function given will be removed from the event's callback queue. If no `callback` argument is given, all callbacks will be removed from the event's callback queue.

on

(eventName, callback) private

Registers a callback to be executed when eventName is triggered

1
2
3
4
5
object.on('event', function(eventInfo){
  // handle the event
});

object.trigger('event');

Parameters:

eventName String
name of the event to listen for
callback Function
function to be called when the event is triggered.

trigger

(eventName, options) private

Use trigger to fire custom events. For example:

1
2
3
4
5
object.on('foo', function(){
  console.log('foo event happened!');
});
object.trigger('foo');
// 'foo event happened!' logged to the console

You can also pass a value as a second argument to trigger that will be passed as an argument to all event listeners for the event:

1
2
3
4
5
6
object.on('foo', function(value){
  console.log(value.name);
});

object.trigger('foo', { name: 'bar' });
// 'bar' logged to the console

Parameters:

eventName String
name of the event to be triggered
options *
optional value to be passed to any event handlers for the given `eventName`