Skip to content

Instantly share code, notes, and snippets.

@dcherman
Created May 2, 2013 17:40
Show Gist options
  • Select an option

  • Save dcherman/5503922 to your computer and use it in GitHub Desktop.

Select an option

Save dcherman/5503922 to your computer and use it in GitHub Desktop.

Revisions

  1. dcherman revised this gist May 2, 2013. 1 changed file with 2 additions and 6 deletions.
    8 changes: 2 additions & 6 deletions main.js
    Original file line number Diff line number Diff line change
    @@ -40,7 +40,7 @@
    });

    // Grab the routing data and product data from Website
    require(
    define(
    ['config.session'],
    function(sessionConfig) {

    @@ -80,11 +80,7 @@
    // Finally: Generically bootstrap this page based on its name!
    // NOTE: We can adapt this technique to be based on the page's full path,
    // a configuration value in "config.page.js", etc.
    define(['Products/' + sessionConfig.product.name + '/pages/' + pageName], function() {
    // Return an empty module (just because `define` calls are mandatory
    // to get proper error reporting in oldIE)
    return {};
    });
    require(['Products/' + sessionConfig.product.name + '/pages/' + pageName]);

    }
    );
  2. @JamesMGreene JamesMGreene created this gist May 2, 2013.
    94 changes: 94 additions & 0 deletions main.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,94 @@
    (function() {

    // Override RequireJS's global error handler
    requirejs.onError = (function(errback) {
    return function require$customOnError(err) {
    console.log('RequireJS error!\nType: ' + err.requireType + '\nModules: ' + JSON.stringify(err.requireModules));

    // Delegate the rest of the errors
    if (typeof errback === 'function') {
    errback(err);
    }
    else {
    throw err;
    }
    };
    })(requirejs.onError);

    // Configure the bare minimum
    require.config({

    // Necessary to support craptacular IE
    enforceDefine: true,

    // `paths` config is relative to the `baseUrl`, and never includes a
    // ".js" extension since the `paths` config could be for a directory.
    //
    // NOTE:
    // All of the following are the only non-static data/configuration(s) to be served from Website
    // If they can't be cached at all (e.g. via a session-based cache key), then we could also
    // combine them into a single resource per page (i.e. 'config.page' would contain the others).
    // They will be requested from the Website domain rather than being relative to the `baseUrl`
    // because their paths start with a '/' (not that they still do NOT end with '.js', though).
    paths: {
    'config.routing': '/new-magically-generated-js/config.routing',
    'config.session': '/new-magically-generated-js/config.session',
    'config.user': '/new-magically-generated-js/config.user',
    'config.page': '/new-magically-generated-js/config.page'
    }

    });

    // Grab the routing data and product data from Website
    require(
    ['config.session'],
    function(sessionConfig) {

    // Configure more
    require.config({
    // By default (from now on), load modules from the StaticContent server instead.
    // NOTE: This is actually done automatically if including the `data-main` attribute on
    // this script file's script tag element on the page, so explicitly setting it here
    // is not even necessary!

    paths: {
    'config.platform': 'Platform/config.platform',
    'config.product': 'Products/' + sessionConfig.product.name + '/config.product'
    }
    });


    // Chain load the additional configuration
    require(
    ['config.platform', 'config.product'],
    function(platformConfig, productConfig) {

    // Merge in the Platform's RequireJS configuration, if any
    if (platformConfig.requirejs) {
    require.config(platformConfig.requirejs);
    }
    // Merge in the Product's RequireJS configuration, if any
    if (productConfig.requirejs) {
    require.config(productConfig.requirejs);
    }

    // Generically find this page's name, i.e. `pageName` === 'new.html'
    var pageName = (function(urlPath) {
    return urlPath.substring(urlPath.lastIndexOf('/') + 1);
    })(window.location.pathname);

    // Finally: Generically bootstrap this page based on its name!
    // NOTE: We can adapt this technique to be based on the page's full path,
    // a configuration value in "config.page.js", etc.
    define(['Products/' + sessionConfig.product.name + '/pages/' + pageName], function() {
    // Return an empty module (just because `define` calls are mandatory
    // to get proper error reporting in oldIE)
    return {};
    });

    }
    );
    }
    );

    })();