Skip to content Skip to sidebar Skip to footer

Callback Before Requirejs "define" Function Is Called?

I'm new to Backbone, and i'm helping to maintain an app that has lots of Backbone and RequireJS code that does something like so: define( ['backbone', 'underscore'], func

Solution 1:

The simplest would probably to define a requireJS module named (e.g.) rawUnderscore which returns Undercore without any configuration.

Then, create a new module named underscorewhich requires rawUnderscore and configures it before returning its value.

Solution 2:

Here's how to make this work as suggested in another answer on this page, and copied from this StackOverflow answer to a similar question:

main.js

requirejs.config({
    paths: {
        'underscore': 'underscoreConfig',
        'originalUnderscore': 'underscore'
    },
    shim: {
        'originalUnderscore': {
            exports: '_'
        }
    }
});

underscoreConfig.js

define(['originalUnderscore'], function(_) {
    _.templateSettings =
    {
        evaluate    : /<%([\s\S]+?)%>/g,
        interpolate : /<%cleanHtml([\s\S]+?)%>/g,
        escape      : /<%[=-]([\s\S]+?)%>/g
    };
    return _;
});

Solution 3:

Another approach (completely untested) may be to define a "require" variable before require.js is included, as detailed here in the Require.JS docs, and here as well

<script>varrequire = {
        deps: ["some/module1", "my/module2", "a.js", "b.js"],
        callback: function(module1, module2) {
            //This function will be called when all the dependencies//listed above in deps are loaded. Note that this//function could be called before the page is loaded.//This callback is optional.
        }
    };
</script><scriptsrc="scripts/require.js"></script>

Post a Comment for "Callback Before Requirejs "define" Function Is Called?"