Service

Services are sets of methods and tools that you can use in your modules. Services are supposed to be reused by multiple modules. Therefore, if there are functions in your modules that are repeated, then it is better to put them into the service

How to create a service #

$K.add('service', 'myservice', {
    init: function(app)
    {
        this.app = app;
    },
    myMethod: function()
    {
        // do something
    }
});

Now you can use this service in your module. Note: before using the service, it must be defined at the module initialization.

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

        // define built-in services
        this.utils = app.utils;
        this.animate = app.animate;

        // define your own service
        this.myservice = app.myservice;
    },
    start: function()
    {
        // use service
        this.myservice.myMethod();
    }
});

Stopping service #

To execute the service stop logic, you can add the stop method. This method will be launched automatically, if the entire application is stopped, see App API.

$K.add('service', 'myservice', {
    init: function(app)
    {
        this.app = app;
    },
    stop: function()
    {
        // stopping
    },
    myMethod: function()
    {
        // do something
    }
});