3
votes

I am having an issue with an undefined variable in my module set up, best to explain by example, I have:

common.js (config for requirejs)

require([
    'module/polyfills/someModule'
], function(
    module
) {

    module.init();

});

module/polyfills/someModule.js

define([
    'underscore',
    'config',
    'text!tpl/utils/textTemplate.html'
    ], function(_, config, template) {

    // ref 1
    return {
        init: function() {
            // ref 2
            // do stuff
        },

        events: function() {
            // add some events
        },

    };
});

If I put a breakpoint at the comment ref 1 I can see the config variable and its properties. However if I put a breakpoint at ref 2 then config is undefined. However underscore and the template are not undefined.

I have removed anything special about config in my require configuration. Config looks like this:

config.js

define([], function (clickType) {
    return {
        clickType: 'test'
    };
});

There are no errors in console and I am very certain this is not a circular dependency!

1
In what browser does this happen? And please add the complete code of init to your question. - Louis
^+1, you might be using template inside your init, because of which it becomes available forming the closure. And that may not be case with config. - Vishwanath

1 Answers

0
votes

the comment place at ref2 is a closure where you haven't accessed config. The function is created at parse and it is a closure, whatever you access inside it is available. simple test, add the following line of code at //ref2

var myConfig = config;

Now you will see config as defined.