- jsLights
- .addHook(name, cb, options)
- .callHooks(name, ref) ⇒
*
- .on(evt, listener) ⇒
Object
- .trigger(evt, args) ⇒
Object
- .unbind(eventObj)
- .assign(path, reference, dependency) ⇒
register_instance
- .after(dependencies, callback)
- .createInstances(config)
- .register(path, reference) ⇒
register_instance
- .extend(path, reference) ⇒
register_instance
- .inspect(path)
- .instantiate(path, reference) ⇒
register_instance
Registering hook by name
Param | Type | Description |
---|---|---|
name | String |
Hook name |
cb | function |
callback function |
options | Object |
aditional options |
options.priority | Object |
hooks with greater priority will be called first (default is 0) |
Example
My.singleton.addHook('onFetchingResults', this.myFunction, { priority: 10 });
Calling hooks for given name ordered by priority If hook is not returning false, execution stops
Returns: *
- return value from hook
Param | Type | Description |
---|---|---|
name | String |
calling hooks registerd with given name |
ref | * |
argument passed in hook |
Example
this.callHooks('onFetchingResults', {myParameter: 5});
Adds a listener function to the specified event
Returns: Object
- object with callback reference which can be used for unbinding
Param | Type | Description |
---|---|---|
evt | String |
name of the event to attach the listener to |
listener | function |
Method to be called when the event is emitted |
Example
My.singleton.on('notification', function(data) {
// callback when notification event is triggered
});
Emits an event of your choice when emitted, every listener attached to that event will be executed.
Returns: Object
- current instance of EventEmitter for chaining
Param | Type | Description |
---|---|---|
evt | String |
name of the event to emit and execute listeners |
args | * |
optional argument to be passed to each listener |
Example
this.trigger('notification', { title: "Hello, its me." });
Removes a listener function based on eventObject returned from method .on()
Param | Type |
---|---|
eventObj | Object |
Example
this._onNotifyReference = this.on("notification", function() {});
this.unbind(this._onNotifyReference);
jsLights.assign(path, reference, dependency) ⇒ register_instance
Assigning reference in passed path
Param | Type | Description |
---|---|---|
path | String |
(for example app.my.function) |
reference | * |
to be assigned (usually a function or class) |
dependency | String/Array |
(optional) if given, reference will be assigned after passed dependencies |
Example
var hello = function() {
console.log("hello")
}
jsLights.assign('My.Namespce.HelloFunction', hello);
// jsLights.assign is shorthand for:
jsLights.register(path, reference).after(dependencies).assign()
Execute after passed dependencies If all dependencies are already triggered, callback is executed immediately otherwise, callback is executed when all dependencies have been triggered
Param | Type |
---|---|
dependencies | String/Array |
callback | function |
Example
jsLights.after(['My.Namespace1', 'My.Namespace2'], function() {
// My.Namespace1 and My.Namespace2 have been created
});
Creating singleton instances using config structure
Param | Type |
---|---|
config | Object |
Example
jsLights.createInstances({
'app.mySingleton': 'app.MyClass',
'app.somethingElse': 'app.AnotherClass'
});
jsLights.register(path, reference) ⇒ register_instance
Registering reference at given path
Param | Type | Description |
---|---|---|
path | String |
(for example app.my.function) |
reference | * |
to be assigned (usually a function or class) |
Example
jsLights.register('My.Namespace.Child').extends('My.Namespace.Parent', Parent => class extends Parent {
// child extends parent
}).after("My.Namespace1").execute();
// without arrow function
jsLights.register('My.Namespace.Child').extends('My.Namespace.Parent', function(Parent) {
return class extends Parent {
// child extends parent
}
}).after("My.Namespace1").execute();
jsLights.extend(path, reference) ⇒ register_instance
Extending class assigned in path with passed reference Shorthand for jsLights.register(path, reference).extends(path)
Param | Type | Description |
---|---|---|
path | String |
(for example app.my.function) |
reference | function |
to be assigned (must be a function returning class) |
Checking status for passed path is it triggered or pending, showing status for all dependencies
Param | Type | Description |
---|---|---|
path | String |
(for example app.my.function) |
Example
jsLights.inspect('My.Namespace1')
jsLights.instantiate(path, reference) ⇒ register_instance
Creating instance from passed reference and assigning it in path Shorthand for jsLights.register(path, reference).after(dependencies).instantiate()
Param | Type | Description |
---|---|---|
path | String |
(for example app.my.function) |
reference | function |
from which instance is created |
after(path) ⇒ register_instance
registration will be executed after passed dependencies have been triggered
Param | Type | Description |
---|---|---|
path | String/Array |
(for example app.my.function) |
Example
jsLights.register('My.Namespace.Child').extends('My.Namespace.Parent', function(Parent) {
return class extends Parent {
// child extends parent
// after triggered dependencies My.Namespace1 and My.Namespace1
}
}).after(["My.Namespace1", "My.Namespace2"]).execute();
Assigning reference (end of chain)
Example
jsLights.register('My.Namespace1', myFunction).after('My.Namespace2').assign()
before(path) ⇒ register_instance
registration will be executed before passed dependencies have been triggered
Param | Type | Description |
---|---|---|
path | String/Array |
(for example app.my.function) |
Example
jsLights.register('My.Namespace.Child').extends('My.Namespace.Parent', function(Parent) {
return class extends Parent {
// child extends parent
// after triggered dependencies My.Namespace1 and My.Namespace1
}
}).after("My.Namespace1").before("My.Namespace2").execute();
dependency(path) ⇒ register_instance
(alias of after) registration will be executed after passed dependencies have been triggered
Param | Type | Description |
---|---|---|
path | String/Array |
(for example app.my.function) |
Example
jsLights.register('My.Namespace.Child').extends('My.Namespace.Parent', function(Parent) {
return class extends Parent {
// child extends parent
// after triggered dependencies My.Namespace1 and My.Namespace1
}
}).after(["My.Namespace1", "My.Namespace2"]).execute();
On passed dependencies, reference will be assigned (end of chain)
Example
jsLights.register('My.Namespace', SampleFunction).after(dependencies).execute()
On passed dependencies, reference will be assigned with passed id (end of chain)
Param | Type |
---|---|
id | String |
Example
jsLights.register('My.Namespace', SampleClass).after(dependencies).executeAs('My:Class')
extends(id, callback) ⇒ register_instance
Extending class using namespace path
Param | Type | Description |
---|---|---|
id | String |
|
callback | function |
that should return new class |
Example
jsLights.register('My.Namespace.Child').extends('My.Namespace.Parent', function(Parent) {
return class extends Parent {
// child extends parent
}
}).execute();
id(id) ⇒ register_instance
Assigning id on registered reference
Param | Type |
---|---|
id | String |
Example
jsLights.register('My.Namespace', SampleClass).after(dependencies).id('My:Class').execute()
On passed dependencies, instance of registered function will be created (end of chain)
Param | Type | Description |
---|---|---|
params | * |
params passed in registered function |
Example
jsLights.register('My.Namespace', SampleClass).after(dependencies).instantiate()