Creating Cluster Modules - sjonesyodle/Cluster GitHub Wiki

A Cluster Module is basically an Object Literal that must contain a function property named init. Ther can be many Clusters in one script, disallowing access to _pubsub messages between Clusters.

Let's examine an example module.

{
    config: {
        // Set a message to send to the console
        message: "This is a message"
    },
    init: function () {
        // Call the myFunc function
        this.myFunc();
    },
    myFunc: function () {
        // Log the message from the config to the console
        console.log(this.config.message);
    }
}

That about sums it up (except for the use of the ._pub() & ._sub() methods).

NOW! We have to tell Cluster to use this object. For that we use the .collect() method:

var cluster = Cluster();
cluster.collect(Array);

One effective method of gathering Arrays to collect is using the .push() method:

var cluster = Cluster(),
    _mods = []; // An empty Array

_mods.push({
    init: function () {
        // Do something
    }
});

_mods.push({
    init: function () {
        // Do something else
    }
});

cluster.collect(_mods).start();

The .start() method above simply calls the init function of each module.

There is no option to name a Module, or give it a unique ID. this is to keep the Modules coupled only by any enhancements, and the _pubsub methods.