C
C
Cat Anton2014-10-17 01:24:27
JavaScript
Cat Anton, 2014-10-17 01:24:27

How to combine different ways of loading modules in RequireJS?

There is such code (modules module1, module2 and module3 are loaded in parallel):

define(['module1', 'module2', 'module3'], function (module1, module2, module3) {
    return { /* ... */ };
});

There is this one (modules module1 and module2 are loaded sequentially, and module3 is dynamically loaded on demand):
define(function (require) {
    var module1 = require('module1');
    var module2 = require('module2');
    return { 
        loadModule3: function () {
            var module3 = require('module3');
        }
    };
});

How can you combine these two approaches and have modules 1 and 2 loaded in parallel like in the first example, and module 3 dynamically like in the second example (without creating additional modules)?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
movetz, 2014-10-22
@27cm

I did this:

define(['module1', 'module2'], function (module1, module2) {
    return { 
        require(['module3'], function(module3){
                /*...*/
        });
     };
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question