quailjs/quail

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

Opened this issue · 0 comments

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);
    });
  }
},