Modules

A Bronson module contains callbacks for onLoad, onStart, onStop, onUnload each providing logical execution points during an application modules lifecycle.

  • onLoad: called with passed in data object automatically after RequireJS loads this module.
  • onStart: called automatically if autostart is enabled or alternatively through Bronson.start(MODULE_ID)
  • onStop: called via Bronon Api Bronson.stop(MODULE_ID)
  • onUnload: called via Bronson API Bronson.unload(MODULE_ID)

var Module = Bronson.Module.extend({
  onLoad: function(data) {
    // initialize module
    console.log(this.data); // data object passed in
    console.log(this.id); // module_id
    console.log(this.started); // whether or not this module has been started
  },
  onStart: function() {
    // used to enabled module and its interactions with other modulesk
  },
  onStop: function() {
    // used to disable module and its interactions with other modules
  },
  onUnload: function() {
    // used to place cleanup code for your module
  }
});

load([object:module])

The module load() method loads a Bronson Module with RequireJS and passes in a set of options and a data object for use in the onLoad callback.

Below is full example of all the options to pass to load along with the defaults.

Bronson.load(
  {
    id: 'foo', // module_id [required]
    path: 'path/to/module', // requirejs path to module [required]
    options: {
      autoload: true,
      autostart: false,
      permissions: false
    }
    data:
      el: '#main' 
    success: function(module) {
      console.log(module); // the loaded module
    },
    error: function(error) {
      console.log(error); // error message if error occurred during load
    }
  }
);

Alternatively you can pass an array of objects module to load

Bronson.load([
  {
    id: 'foo', // module_id [required]
    path: 'path/to/module', // requirejs path to module [required]
    ...
  }, 
  {
    ...
  }
])

unload(string:moduleId)

The module unload() method unloads the module with the supplied moduleId and calls the onUnload callback of the module for cleanup.

Bronson.unload(MODULE_ID);

unloadAll()

The module unloadAll() method unloads all loaded modules by unregistering them with RequireJS and finally calling each of their onUnload callbacks.

Bronson.unloadAll();

start(string:moduleId)

The module start() method starts a module with the supplied moduleId that has already loaded method and not previously started.

Bronson.start(MODULE_ID);

startAll()

The module startAll() method starts all modules that have not been previously started.

Bronson.startAll();

stop(string:moduleId)

The module stop() method stops a module with the supplied moduleId that has been started.

Bronson.stop(MODULE_ID);

stopAll()

The module stopAll() method stops all modules that have been previously started.

Bronson.stopAll();

find(string:moduleId)

The module find() method returns a Bronson module that matches the supplied moduleId.

Bronson.find(MODULE_ID);

findAll()

The module findAll() method returns an array of all Bronson modules.

Bronson.findAll();

Publish & Subscribe

Bronson comes with built in methods for pubsub module communication. Bronson's pubsub adheres to a message naming convention for easy unsubscribing and optimized publishing.

publish(channel:topic, message)

  • string:channel:topic the channel:topic to publish to
  • object:message the object to pass to subscribers of that channel:topic

The publish() publishes an event to all subscribers utilizing the pattern channel:topic in addition to an message object to pass to each subscriber.

For instance if we had an application wide channel and we wanted to notify all of our subscribers of that channel that a geoposition is changing we could do the following.

Bronson.publish('app:geoupdate', { lat: 37.788086, lng: -122.401111});

subscribe(subscriber:channel:topic, [callback])

  • subscriber:channel:topic the pattern identifing who is subscribing to what channel and on what topic
  • callback the callback to execute upon publishing on this subscribed channel:topic

The publish() publishes an event to all subscribers utilizing the pattern channel:topic in addition to an message object to pass to each subscriber.

For instance if we had an application wide channel and we wanted to notify all of our subscribers of that channel that a geoposition is changing we could do the following.

Bronson.subscribe('module:app:geoupdate', function(msg) { console.log(msg); });

unsubscribe(subscriber:channel:topic)

  • subscriber:channel:topic the event to unsubscribe to

The unsubscribe() method unsubscribes a subscriber from an event. The unsubscribe() method has three levels of specificity.

  1. By supplying just a subscriber all events that the subscriber are subscribed to will be removed
  2. By supplying a subscriber and a channel subscriber:channel all events in that channel for that subscriber will be removed.
  3. By supplying a subscriber a channel and a topic subscriber:channel:topic just that single topic in for that subscriber will be removed.
Bronson.unsubscribe('module:app:geoupdate'); // removes just this topic
Bronson.unsubscribe('module:app'); // removes all topics in this channel
Bronson.unsubscribe('module'); // removes all events for this subscriber

Permissions

DESCRIPTION TEXT

load()

SUB DESCRIPTION TEXT