Skip to content

Modules

Jeroen Reurings edited this page Dec 7, 2013 · 2 revisions

Glue modules

Glue uses the Asynchronous Module Definition to define her modules. This means that Glue is written in a modular way. Glue modules are written using the module pattern.

Create a Glue module

To create a Glue module you can use the global "glue" object. You can call the "module" object which contains the "create" method. The "create" method needs to have two parameters. The first is the name of the module and the second one is the function that represents the actual module. You can create a new Glue module in the following way:

glue.module.create('glue.documentation.test', function () {
    return {
        init: function () {
            console.log('Init test module');
        }
    };
});

As you can see we name our module 'glue.documentation.test'. The 'glue.documentation' part can be seen as the namespace. You can add multilevel namespaces separated by dots. The word after to last dot always represents the actual module name.

The second parameter is the actual module function. In the example above we let the function return an object literal which contains an init method.

Get a Glue module

To get an already created Glue module you can use 'glue.module.get'. Instead of passing a string as the first parameter, you need to pass in an array with strings. The module will then automatically be passed to the module function, as you can see in the example below:

glue.module.get(['glue.documentation.test'], function (Test) {
    // call the init method on the the module
    Test.init();
});

To get multiple Glue modules, you can pass multiple strings in the array. The modules will then be passed to the function in the same order, like in the following example:

glue.module.get(['glue.documentation.test', 'glue.namespace.other'], function (Test, OtherModule) {
    // call the init method on the the module
    Test.init();
    // you also have access to the other module
    OtherModule.someMethod();
});

Clone this wiki locally