API

In addition to the communication between modules at the event level, there is sometimes a need to access the public methods of the module directly. To do this, the Kube has the ability to call the API.

Services #

Access to public service methods is available from any module, if the service is declared in the init method of the module.

(function($K)
{
    $K.add('module', 'mymodule', {
        init: function(app, context)
        {
            this.app = app;

            // declare built-in service
            this.animate = app.animate

            // declare your service
            this.myservice = app.myservice;

            // build module
            this._build();
        },
        _build: function()
        {
            // use services api
            this.myservice.mymethod();
            this.animate.run($K.dom('#myelement'), 'fadeIn');
        }
    });
})(Kube);

Access to other services in the services occurs without a declaration, because they are automatically declared by the Kube application.

(function($K)
{
    $K.add('service', 'myservice', {
        init: function(app)
        {
            this.app = app;
        },
        show: function()
        {
            // use services api without declaration in init method
            this.animate.run($K.dom('#myelement'), 'fadeIn');
        }
    });
})(Kube);

Modules #

Architecture Kube is designed to ensure that the modules are always independent of each other. That is why the interaction between them must be built on the basis of sending and capturing events. In this case, the absence of any module will not stop the entire application. Since, if there is no module, the capturing of its event will be ignored without generating an error.

In addition to the communication between modules at the event level, there is sometimes a need to access the public methods of the module directly. To do this, the Kube has the ability to call the this.app.api method.

For example, to open an alert, you can use the open method of this module.

this.app.api('alert.open');

In this case, if there are alert modules on the page, they will all be opened. If you need to open only a specific alert, then when you call api, you need to specify the ID of its element or the data-name attribute.

For example, if there is the element with ID on the page.

<div id="myalert" data-kube="alert" class="alert is-hidden"></div>

Or with the data-name attribute.

<div data-name="myalert" data-kube="alert" class="alert is-hidden"></div>

You can call a method of the specific alert like this:

this.app.api('alert.myalert.open');

Return values #

To ensure the independence of the modules, the this.app.api method does not return values.

Therefore, if there is no called module or this module does not have a called method, the API call will be ignored without stopping the entire application.

If you need to get a value from the module's methods, then this option is available when you are catching the module event.

For example, by capturing a Tabs module event, you can get an active/open tab element.

ontabs: {
    opened: function(module)
    {
        // get active tab and box
        var $tab = module.getActiveTab();
        var $box = module.getActiveBox();
    }
}

Call API from outside #

There is the possibility of access to the methods of modules from outside. You can use $K.api for that.

<button onclick="$K.api('alert.open');">Open</button>

In this case, if there are alert modules on the page, they will all be opened. If you need to open only a specific alert, then when you call api, you need to specify the ID of its element or the data-name attribute.

For example, if there is the element with ID on the page.

<div id="myalert" data-kube="alert" class="alert is-hidden"></div>

Or with the data-name attribute.

<div data-name="myalert" data-kube="alert" class="alert is-hidden"></div>

You can call a method of the specific alert like this:

<button onclick="$K.api('alert.myalert.open');">Open</button>

Send arguments #

When you call the module methods, you can send arguments.

this.app.api('mymodule.open', arg1, arg2);

Or when you are calling methods from outside.

<button onclick="$K.api('mymodule.open', arg1, arg2);">Open</button>