Skip to content

WIP: Added a new dispatcher

Luke "Jared" Bennett requested to merge new-dispatcher-es6-idea into master

What does this MR do?

THIS NOW WORKS BUT WIP WHILST PERFECTING 👍

This dispatcher lets you register 'modules'. Modules are functions that are invoked when the current page identifier (found in the body's data-page attribute) matches the modules accompanied page queries. You can pass an array of page queries as well as a single page query. You can also use wildcards in page queries using the standard asterisk notation.

During the registration process, the module is also added to the window.gl property.


You can pass a single page query to invoke your module on a specific page.

Example:

class Animal {
  constructor() {
  }
}

window.gl.Dispatcher.register('animal:facts', Animal);

console.log(window.gl.Animal);
/**
 *  function class Animal {
 *    constructor() {
 *    }
 *  }
 */

The first register parameter can be an array to invoke a module across multiple page identifiers. It can also use wildcards.

Example:

class Animal {
  constructor() {
  }
}

window.gl.Dispatcher.register([
  'animal:facts',
  'animal:opinions:philosophical',
  'animal:search:*',
  'animal:*:anatomy'
], Animal);

The registered module can also provide a destroy prototype method that will be called before DOM nodes are removed.

Example:

class Animal {
  constructor() {
    $(document).on('click', clickEventHandler);
    $(body).on('scroll', scrollEventHandler);
  }

  destroy() {
    $(document).off();
    $(body).off();
  }
}

window.gl.Dispatcher.register('*', Animal);

Lastly, if you need to add parameters to constructors...

class Animal {
  constructor(param) {
    console.log(param);
  }
}

window.gl.Dispatcher.register('*', () => new Animal('param'));

Why was this MR needed?

Dispatcher is getting crazy and needs a new pattern. Too many epic features! The javascript codebase is sure to accelerate in size and more specifically, modularity.

What are the relevant issue numbers?

#13719 (closed).

Does this MR meet the acceptance criteria?

Merge request reports