Reputation: 71961
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'],
function(Backbone, _) {
// some code here
}
);
I'd like to configure Underscore in each of these pages... In the most simple sense, I could just do something like so:
define(
['backbone', 'underscore'],
function(Backbone, _) {
// do some stuff to configure underscore
configureUnderscore(_);
}
);
but I'd like to leave each page's define
function alone, and just inject a callback that's called before the define
callback is called.
How can this be done with Backbone or RequireJS?
Upvotes: 2
Views: 400
Reputation: 71961
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>
var require = {
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>
<script src="scripts/require.js"></script>
Upvotes: 0
Reputation: 71961
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:
requirejs.config({
paths: {
'underscore': 'underscoreConfig',
'originalUnderscore': 'underscore'
},
shim: {
'originalUnderscore': {
exports: '_'
}
}
});
define(['originalUnderscore'], function(_) {
_.templateSettings =
{
evaluate : /<%([\s\S]+?)%>/g,
interpolate : /<%cleanHtml([\s\S]+?)%>/g,
escape : /<%[=-]([\s\S]+?)%>/g
};
return _;
});
Upvotes: 1
Reputation: 25994
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 underscore
which requires rawUnderscore
and configures it before returning its value.
Upvotes: 1