The Inject Function - sjonesyodle/Cluster GitHub Wiki
The new Inject method allows us to easily add new Modules to Cluster, after the .start()
method has been called.
This new method should be passed the same Object Literal as the .collect()
method.
Injecting a single new Module can be done like so:
Cluster.inject({
init: function () {
console.log("I am a new Module!");
}
});
Also, there are three ways to pass multiple Modules to the .inject()
method:
- Array of Object Literals:
Cluster.inject([{/* Module 1 */}, {/* Module 2 */}]);
- A List of Module-Objects as arguments:
Cluster.inject({/* Module 1 */}, {/* Module 2 */});
- Calling the method in a chain:
Cluster.inject({/* Module 1 */}).inject({/* Module 2 */});
Consider the tests below:
var c = Cluster();
// Run the initial collect and start methods.
c.collect([{
init: function () {
console.log(1);
}
}, {
init: function () {
console.log(2);
}
}]).start();
// #1 Array of Object Literals
c.inject([{
init: function () {
console.log(3);
}
}, {
init: function () {
console.log(4);
}
}]);
// #2 List of Module-Objects as arguments
c.inject({
init: function () {
console.log(5);
}
}, {
init: function () {
console.log(6);
}
});
// #3 Chaining the methods
c.inject({
init: function () {
console.log(7);
}
}).inject({
init: function () {
console.log(8);
}
});