Skip to content Skip to sidebar Skip to footer

Load Standard Javascript Files With Requirejs

I'm trying out requireJS in order to improve the loading of Javascript on an ASP.NET MVC app, using Knockout. I have some files defining custom ko bindings like that: (function (ko

Solution 1:

Thanks to this answer, I managed to inject Knockout back in the global namespace, making it available to legacy Javascript files that needed it.

First, create a module that injects ko in the global namespace:

define('knockout.inject', ['knockout'], function (k) {
    window.ko = k;
    return k;
});

Then, map the module to knockout to execute it for every knockout dependency.

var require = {
    baseUrl: "/Scripts",
    paths: {
        //..."knockout": "knockout-3.3.0.debug",
        "knockoutbindings": "knockout.bindings",
    },
    shim: {
        "knockoutbindings": {
            deps: ["knockout"]
        }
    },
    map: {
        // inject ko back in the global namespace'*': {
            'knockout': 'knockout.inject'
        },
        // prevent cycles'knockout.inject': { 'knockout': 'knockout' }
    }
};

Post a Comment for "Load Standard Javascript Files With Requirejs"