modules.define

Static function.

Defining a module in the module system.

Note

We recommend creating your custom modules in your own namespace, to avoid accidentally replacing the modules that are necessary for the API to work.

Returns self-reference.

{ modules } modules.define(module[, depends, resolveCallback[, context]])

Parameters:

Parameter

Default value

Description

module*

Type: String

Module name.

depends

Type: String[]

Array of names of necessary modules. This argument may be omitted.

resolveCallback*

Type: Function

Function that defines the module. The first argument in resolveCallback will be a provide function, to which you will need to pass a module. The provide function call can be delayed. Subsequent arguments are the modules specified in the dependencies. The order of the modules will match the order in the depends array. If a module cannot be resolved, the module system must be notified. This can be done by passing a second argument to the provide function. The second argument will be passed to errorCallback and promise as an error in the module request. As a result, the module can be requested again.

context

Type: Object

Context for the function.

* Mandatory parameter/option.

Examples:

1.

// Defining a custom module. 'plugin.*' is a custom namespace.
ymaps.modules.define('CustomModule', function (provide) {
    var CustomModule = function (defValue) {
        this.field = defValue;
    };
    provide(CustomModule);
});
// Requesting modules
ymaps.modules.require(['CustomModule'])
    .spread(
        function (CustomModule) {
            // ...
        },
        this
   );

2.

// Defining a custom asynchronous module.
ymaps.modules.define('CustomAsyncModule', function (provide) {
	// For the module to work, we must load an external script.
    $.getScript( "ajax/test.js")
        .done(function( script, textStatus ) {
            function CustomAsyncModule () {
                // ...
            }
        // Calling the provide function after loading the script.
        provide(CustomAsyncModule);
    });
});
// Requesting modules
ymaps.modules.require(['CustomAsyncModule'])
    .spread(
        function (CustomAsyncModule) {
            // ...
        },
        this
    );

3.

// Creating a custom asynchronous module with consideration for an error.
ymaps.modules.define('plugin.CustomModule', function (provide) {
    $.getScript( "ajax/test.js" )
        .done(function( script, textStatus ) {
            // Processing successful script loading.
            provide({ a: 1 });
        })
        .fail(function( jqxhr, settings, exception ) {
            // Notifying the module system that the module can't be prepared right now.
            provide(null, new Error('Error when loading'));
        });
});

// Requesting modules with error handling.
ymaps.modules.require(['plugin.CustomModule'])
    .spread(
        function (CustomModule) {
            // ... 
        },
        function (error) {
            console.log(error.messsage); // "Error when loading".
            // Code for handling the module loading error. You can request the module one more time.
        },
        this
    );

4.

// Defining a custom module with dependencies.
ymaps.modules.define('CustomLayoutModule', [
     'templateLayoutFactory', 
     'layout.storage'
], function (provide, templateLayoutFactory, layoutStorage) {
    var customLayoutClass = templateLayoutFactory.createClass(
        '<div>My Layout</div>',
        {
            build: function () {
                 customLayoutClass.superclass.build.call(this);
                 // ...
            },
            clear: function () {
                 // ...
                 customLayoutClass.superclass.clear.call(this);
            }
        }
    );
    layoutStorage.add('customLayout', customLayoutClass);
    provide(customLayoutClass);
});
// Requesting a custom layout
ymaps.modules.require(['CustomLayoutModule'])
    .spread(
        function (CustomLayoutModule) {
            // ...
        },
        this
   );