1
votes

What is the benefit of defining 3rd party libraries (JQuery/Underscore/Backbone) as modules and using those as dependencies?

require(["jquery", "underscore"], function($, _) {
    // Use $ and _ in here
});

Underscore for example creates a global '_' variable, that I could just as easily use within the function above, assuming underscore is included prior to that function.

To be used by Require, Underscore requires the code to be modified to return a value, or a shim defined. Why bother, when I can just include it via a script tag?

I get that it provides a certain level of indirection and allows me to map other dependencies to those same variables, and have it scoped locally to that function. However, I don't see this ever being useful for these types of 3rd party libraries that form the core of the application.

2

2 Answers

2
votes

After building some apps with Backbone/requireJs I can see no disadvantage in building a backbone app with requireJs, where the main dependencies are simple loaded via an old school script tag.

You have to load this requirements anyway in the first place, its used by the most of your modules and you will probably never exchange it with another framework. So there is no afford for the boilerplate code in every module.

0
votes

You don't have to change the library to use it in AMD loader.

require.config({paths:{underscore:'//some.cdn/path/to/underscore.js'}})

require(["jquery", "underscore"], function($ /*, note that we don't override _ here */) {
    // Use AMD $ and global _ in here
});