Options

You can set global settings for the entire application and for all modules, as well as you can specify settings for each module separately.

Global options #

Specify the object when Kube is initialized to set global settings.

$K.init({
    myoption: true,
    mysecondoption: 12
});

Now the specified settings are available in any services and modules using the this.opts object.

In services, you simply need to use the opts object, which stores all the settings, for example:

$K.add('service', 'myservice', {
    init: function(app)
    {
        this.app = app;
    },
    myMethod: function()
    {
        if (this.opts.myoption)
        {
            // do something
            console.log(this.opts.mysecondoption);
        }
    }
});

In modules, you must first declare the options object in the init method.

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

        // define options
        this.opts = app.opts;
    }
});

After that, the settings object can be used in any method of the module.

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

        // define options
        this.opts = app.opts;
    },
    myMethod: function()
    {
        if (this.opts.myoption)
        {
            // do something
            console.log(this.opts.mysecondoption);
        }
    }
});

Set options from forms and inputs #

In Kube, it is possible to set global settings from the form fields or inputs that are on the page.

// Form on the page
<form id="myform">
    <input type="text" name="myoption" value="true">
    <select name="mysetting">
        <option value="1">Selected</option>
    </select>
</form>

// Kube init
$K.init({
    append: '#my-form'
});

Or you can set up multiple selectors of option sources:

$K.init({
    append: '#my-form, #my-input, .select'
});

As a result, Kube takes the data from the specified inputs and converts them into the option object. Then you can get the options with this.opts in your services and modules.

Module options #

You can specify the module settings using data attributes.

<div data-kube="mymodule" data-value="2" data-second-value="true"></div>

The settings will be read by the module and available in the module context.

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

        // context
        this.context = context;
        this.params = context.getParams();
    },
    myMethod: function()
    {
        if (this.params.value === 2)
        {
            // do something
            console.log(this.params.secondValue);
        }
    }
});

Here is the way to set some default options for the module:

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

        // defaults
        var defaults = {
            value: 0,
            secondValue: false
        };

        // context
        this.context = context;
        this.params = context.getParams(defaults);
    },
    myMethod: function()
    {
        if (this.params.value === 2)
        {
            // do something
            console.log(this.params.secondValue);
        }
    }
});

In this case, the default settings will be replaced with those values that were specified in the data attributes. If data attributes were not specified, then the values will remain default.