Search code examples
require

Require JS with parameter


I am new to ReqireJs. I am trying to do the following:

I have a file(mymodule.js) with the following code:

require([
    'jquery'
], function ($) {
    var name;
    $(document).ready(function() {
       //do some load stuff
    });
});

I am trying to include the file into some other file as follows:

require(['modules/mymodule.js']);

which works fine. My question is that how can I pass some parameters from require(['modules/mymodule.js']); into mymodules.js?

Thanks and regards.


Solution

  • I have found the solution here:

    http://blog.novanet.no/4-strategies-for-passing-parameters-to-requirejs-modules/

    I have used the code from 1st step from above link "Passing parameter to method" as follows:

    define(
        [
            'jquery'
        ], 
        function($){
            return {
                sayHello: function(name){
                    alert("Hello " + name);
                }
            };
        }
    );
    

    Then I have passed the parameter as follows:

    require(['modules/mymodule.js'], function(mymodule){
        mymodule.sayHello("World");
    });