Skip to content
This repository has been archived by the owner on Sep 24, 2019. It is now read-only.

Refactor the custom event bubbling code in the core Classes (e.g. TestCollection) to use a real event pub/sub library #351

Open
jessebeach opened this issue Jun 20, 2015 · 0 comments

Comments

@jessebeach
Copy link
Member

This would either involve factoring out the duplicated code into an internal library or replacing it with a third-party, open-source library.

Here are the methods I'm referring to from TestCollection.

// @todo, make this a set of methods that all classes extend.
listenTo: function (dispatcher, eventName, handler) {
  // @todo polyfill Function.prototype.bind.
  handler = handler.bind(this);
  dispatcher.registerListener.call(dispatcher, eventName, handler);
},
registerListener: function (eventName, handler) {
  // nb: 'this' is the dispatcher object, not the one that invoked listenTo.
  if (!this.listeners[eventName]) {
    this.listeners[eventName] = [];
  }

  this.listeners[eventName].push(handler);
},
dispatch: function (eventName) {
  if (this.listeners[eventName] && this.listeners[eventName].length) {
    var eventArgs = [].slice.call(arguments);
    this.listeners[eventName].forEach(function (handler) {
      // Pass any additional arguments from the event dispatcher to the
      // handler function.
      handler.apply(null, eventArgs);
    });
  }
},
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

1 participant