jComponent / 07. Plugins
Updated: 10. September 2018
Author: Peter Širka

07. Plugins

Professional Support Chat with contributors

+v15 Plugins are targeted for dynamic parts which can be removed when aren't used. Plugins are the best way for dynamic pages, forms, windows, etc. in SPA / Single Page Applications.

Topics:

Good to know:

IMPORTANT: events or watchers declared in the plugin scope will be removed automatically if the plugin is removed

Declaration

  • a plugin name can contain a-z chars only (no numbers or white characters)
PLUGIN('name_of_plugin', function(exports) {

    // You can define your custom functions or properties
    exports.myfunction_1 = function() {
        console.log('something');
    };

    exports.myfunction_2 = function() {
        console.log('something');
    };    

    exports.myfunction_N = function() {
        console.log('something');        
    };

    // 2 way data-binding watcher:
    WATCH('some.path', function(path, value) {
        // ...
    });

    // Events
    ON('some.event', fucntion(req) {
        // ...
    });

});

Properties

PLUGIN('name_of_plugin', function(exports) {

    // A parent element of the <script> element
    // {jQuery}
    exports.element;

    // A plugin name
    // {String}
    exports.name;

});

Delegates

Plugins support only the one delegate destroy. This delegate is executed when the plugin will be destroyed.

PLUGIN('name_of_plugin', function(exports) {

    exports.destroy = function() {
        // is executed when the plugin is destroyed  
    };

});

How to call plugin exports?

This is very easy, you can use EXEC method:

EXEC('name_of_plugin/myfunction_1', arg1, arg2, argN);

How to get instance of some plugin?

You can get instance of the plugin very easy, just call PLUGIN(name) without second argument, for example:

var exports = PLUGIN('name_of_plugin');
if (exports)
    exports.myfunction_1();

Best practices

  • keep Plugins as independent .html files
  • I recommend to use Plugin names with lower-case