Function

Methods

subscribeOnce(publisher, eventTypes, scope) → {function}

Subscribes a method to a list of events. Each event subscribed will only be fired once, after which it is unsubscribed.

Parameters:

Name Type Argument Description
publisher Class

Object or Class on which you want to register the method.

eventTypes String | Array

String or array containing eventTypes to which to subscribe this method to.

scope Object optional 

Scope in which the method will run when the event fires.

Returns:

Method as subscribed.

subscribeTo(publisher, eventTypes, scope) → {function}

Subscribes a method to a list of events

Parameters:

Name Type Argument Description
publisher Class

Object or Class on which you want to register the method.

eventTypes String | Array

String or array containing eventTypes to which to subscribe this method to.

scope Object optional 

Scope in which the method will run when the event fires.

Returns:

Method as subscribed.

unsubscribeFrom(publisher, eventTypes) → {function}

Unsubscribes a method from a list of events

Parameters:

Name Type Description
publisher Class

Object or Class on which you want to unregister the method.

eventTypes String | Array

String or array containing eventTypes from which to unsubscribe this method to.

Returns:

Method as unsubscribed.

Example

var differentScope = {
   version: 112
};
var publisher = {
   version: 102
};
var fnOriginal = function (ev) {
   switch (ev.type) {
      case 'withScope':
         console.log('Running in differentScope scope, version: ', this.version);
         break;
      case 'noScope':
         console.log('Running in publisher scope, version: ', this.version);
         break;
      }
};

//If you subscribed with using a scope, you have to unsubscribe using the returned method.
var fnSubscribed = fnOriginal.subscribeTo(publisher, ['withScope'], differentScope);
fnOriginal.subscribeTo(publisher, ['noScope']);

publisher.fire('withScope');
publisher.fire('noScope');
fnSubscribed.unsubscribeFrom(publisher, ['withScope']);
fnOriginal.unsubscribeFrom(publisher, ['noScope']);