Search code examples
jqueryrequirejsextend

Is it possible to do configuration inheritance with Require.js?


I am trying to figure out the best way to do quasi-inheritance of configuration in Require.js.

I have a root webapp, then child webapps underneath in their own contexts, like this:

/
/configuration
/reporting

The root (/) webapp would have a base Require.js config object, then the configuration and reporting webapps would have their own overrides. I've gotten it to work by using the jQuery extend method, e.g.:

var rootConfig = {
    // lots of stuff here: paths, shim, and baseUrl
}
var localConfig = {
    paths: {
        // this app wants to have a different version, so override
        jqueryui: '/js/lib/ui/jquery-ui-1.8.22.custom.min'
    }
}

var mergedConfig = {};
$.extend(true, mergedConfig, rootConfig, localConfig);

require.config(mergedConfig);

This works fine, but it kind of breaks the Require.js paradigm because I have to load the jQuery library statically outside of Require.js. Any ideas how to do this more elegantly?

I've taken a look at James Burke's multipage examples which is kind of along these lines, but I don't think the point there is to inherit configuration exactly.

Thanks for any help!


Solution

  • I ended up just culling a non-jquery extend function from a couple other sources that does the trick, so I can keep from loading jquery before require.js does it...

    var com = com || {};
    com.my = com.my || {};
    com.my.extend = function (obj1, obj2) {
    for (var p in obj2) {
        try {
            if ( obj2[p].constructor == Object ) {
                obj1[p] = com.my.extend(obj1[p], obj2[p]);
            } else {
                obj1[p] = obj2[p];
            }
        } catch(e) {
            obj1[p] = obj2[p];
        }
    }
    
    return obj1;
    }
    

    ...then use it to extend/merge the configuration objects before calling require.config, e.g.:

    // merge localConfig with the root config
    var com.my.mergedConfig = 
    com.my.extend(com.my.rootConfig, com.my.childConfig);
    
    // apply the configuration to require.js
    require.config(com.my.mergedConfig);
    

    Hope that helps someone else.