Skip to content Skip to sidebar Skip to footer

Require Js Is Ignoring My Config

I'm having pretty simple directory structure for scripts: /js/ <-- located in site root libs/ jquery-1.10.1.min.js knockout-2.2.1.js knockout.

Solution 1:

Solved.

The problem was that config file defined in data-main attribute is loaded asynchronously just like other dependencies. So my config.js accidentally was never completely loaded and executed before require call.

The solution is described in official RequireJS API: http://requirejs.org/docs/api.html#config

... Also, you can define the config object as the global variable require before require.js is loaded, and have the values applied automatically.

So I've just changed my config.js to define global require hash with proper configuration:

varrequire = {
    baseUrl: "/js/libs",
    paths: {
        "jquery":       "jquery-1.10.1.min",
        "knockout":     "knockout-2.2.1",
        "komapping":    "knockout.mapping"
    }
};

and included it just BEFORE require.js:

<scripttype="text/javascript"src="/js/config.js"></script><scripttype="text/javascript"src="/js/require.js"></script>

This approach allows to control script execution order, so config.js will always be loaded before next require calls.

All works perfectly now.

Solution 2:

Fixed the issue.

My config was being loaded asynchronously, and therefore the config paths weren't set before my require statement was being called.

As per the RequireJS docs Link here, I added a script call to my config before the require.js call. And removed the data-main attribute.

varrequire= {
    baseUrl: '/js',
    paths: {
        'jquery':      'vendor/jquery/jquery-2.0.3.min',
        'picker':      'vendor/pickadate/picker.min',
        'pickadate':   'vendor/pickadate/picker.date.min'
    },
    shim: {
        'jquery': {
            exports: '$'
        },
        'picker':    ['jquery'],
        'pickadate': {
            deps:    ['jquery', 'picker'],
            exports: 'DatePicker'
        }
    }
}

All is now working

Post a Comment for "Require Js Is Ignoring My Config"