App
The app service is the core of Kube, runs services and modules. It provides communication between services and modules through events and API.
Events
Event | Description |
---|---|
start |
This event fires immediately when the Kube application start. Before the initialization of modules and services. |
started |
This event is fired when all services, modules, and other application tools are started. |
stop |
This event fires when the method app.stop is called. Before the stopping services and modules.
|
stopped |
This event is fired when all services, modules, and other application tools are stopped. |
Example of catching app events in your module.
(function($K) { $K.add('module', 'mymodule', { init: function(app, context) { this.app = app; }, // catch event onmessage: { start: function() { console.log('app start event'); } } }); })(Kube);
Methods
broadcast
name | String |
modulename.eventname |
module | Object |
instance of the module (typically 'this' object) |
Create an event with the specified name, which can be caught in any other module or in several modules.
(function($K) { $K.add('module', 'mymodule', { init: function(app, context) { this.app = app; }, myBroadcast: function() { this.app.broadcast('mymodule.success', this); this.app.broadcast('mymodule.success', this, arg1, arg2); this.app.broadcast('mymodule.success', this, { "arg1": 1, "arg2": 2 }); } }); })(Kube);
How to catch an event in another module.
(function($K) { $K.add('module', 'myanothermodule', { init: function(app, context) { this.app = app; }, onmessage: { mymodule: { success: function(sender) { console.log('Success!'); } } } }); })(Kube);
The app.broadcast
method will automatically create the second module event,
which will specify the module element ID or the data-name
attribute.
For example, create a module element on the page and assign it an ID attribute.
<div id="mymoduleid" data-kube="mymodule">...</div>
Now we can catch the event by module ID in another module.
(function($K) { $K.add('module', 'myanothermodule', { init: function(app, context) { this.app = app; }, onmessage: { mymodule: { success: function(sender) { if (sender._id === 'mymoduleid') { console.log('Success!'); } } } } }); })(Kube);
api
name | String |
modulename.methodname or modulename.moduleID.methodname |
Call the method of the specified module. If only the module name and the method name are specified, it calls the method of all modules with this name on the page. If a module ID is specified using the id attribute or data-name for an element, it calls the method only for the module with this ID.
The method does not return values, to ensure the independent work of the modules. If the specified module or method is does not exists in the application, the call will be ignored without generating an error.
(function($K) { $K.add('module', 'mymodule', { init: function(app, context) { this.app = app; }, callModule: function() { this.app.api('alert.close'); this.app.api('alert.myalert.close'); } }); })(Kube);
Call from outside.
$K.api('alert.close'); $K.api('alert.myalert.close');
stop
Stop the application. Also calls the stop
method (if exists) for all services and modules.
(function($K) { $K.add('module', 'mymodule', { init: function(app, context) { this.app = app; }, stopMyApp: function() { this.app.stop(); } }); })(Kube);
Call from outside.
$K.api('stop');