Skip to content

2.1 Lunar.createModule

Caio Vaccaro edited this page Apr 16, 2016 · 2 revisions

Lunar(component).createModule()

Description:

This function is the primary hook between your code and Lunar. It is a Factory that returns an object formatted in the way Lunar needs your code to be, in order to help you refactor way less.

Assuming you already have your code organized in modules, each module have properties and methods, as usual. We propose that every method you want to be public have an "action". Actions are constants that are used to:

  • Name your functions, instead of strings, which diminishes refactoring.
  • Serve as unique "topics" in the Mediator.

When you pass your Feature or Component to Lunar and call "createModule", it will:

  • Create a new object with all properties and methods of your original object.
  • Convert your actions to Symbols.
  • Convert all your "action methods" to their symbol correspondent (eg. 'ONE'() will be Symbol('ONE')()).
  • Register all your actions as "topics" in the Mediator.
  • Hook your "action methods" as callbacks of the registered actions.

Example:

import Lunar from 'orbit';
import actions from './actions';

const MyComponent = Lunar({
  actions: actions,
  privateMethod() {...},
  [actions.ONE]() {...},
  [actions.TWO]() {...}
}).createModule();

Returns:

A new object, based on your original object.

{
  actions: { ONE: 'Action description' },
  ONE() {...}
}
// will become
{
  actions: { 'ONE': Symbol('Action description') },
  Symbol('Action description')() {...}
}
Clone this wiki locally