Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
30
rated 0 times [  31] [ 1]  / answers: 1 / hits: 70426  / 13 Years ago, tue, november 15, 2011, 12:00:00

I'm trying to load Backbone and Underscore (as well as jQuery) with RequireJS. With the latest versions of Backbone and Underscore, it seems kind of tricky. For one, Underscore automatically registers itself as a module, but Backbone assumes Underscore is available globally. I should also note that Backbone doesn't seem to register itself as a module which makes it kind of inconsistent with the other libs. This is the best main.js I could come up with that works:



require(
{
paths: {
'backbone': 'libs/backbone/backbone-require',
'templates': '../templates'
}
},
[
// jQuery registers itself as a module.
'http://cdnjs.cloudflare.com/ajax/libs/jquery/1.7/jquery.min.js',

// Underscore registers itself as a module.
'http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.2.1/underscore-min.js'
], function() {

// These nested require() calls are just due to how Backbone is built. Underscore basically says if require()
// is available then it will automatically register an underscore module, but it won't register underscore
// as a global _. However, Backbone expects Underscore to be a global variable. To make this work, we require
// the Underscore module after it's been defined from within Underscore and set it as a global variable for
// Backbone's sake. Hopefully Backbone will soon be able to use the Underscore module directly instead of
// assuming it's global.
require(['underscore'], function(_) {
window._ = _;
});

require([
'order!http://cdnjs.cloudflare.com/ajax/libs/backbone.js/0.5.3/backbone-min.js',
'order!app'
], function(a, app) {
app.initialize();
})
});


I should mention that, while it works, the optimizer chokes on it. I receive the following:



Tracing dependencies for: main
js: /home/httpd/aahardy/requirejs/r.js, line 7619: exception from uncaught JavaScript throw: Error: Error: Error evaluating module undefined at location /home/httpd/aahardy/phoenix/trunk/ui/js/../../ui-build/js/underscore.js:
JavaException: java.io.FileNotFoundException: /home/httpd/aahardy/phoenix/trunk/ui/js/../../ui-build/js/underscore.js (No such file or directory)
fileName:/home/httpd/aahardy/phoenix/trunk/ui/js/../../ui-build/js/underscore.js
lineNumber: undefined
http://requirejs.org/docs/errors.html#defineerror
In module tree:
main


Is there a better way of handling this? Thanks!


More From » backbone.js

 Answers
3

RequireJS 2.X now organically addresses non-AMD modules such as Backbone & Underscore much better, using the new shim configuration.



The shim configuration is simple to use: (1) one states the dependencies (deps), if any, (which may be from the paths configuration, or may be valid paths themselves). (2) (optionally) specify the global variable name from the file you're shimming, which should be exported to your module functions that require it. (If you don't specify the exports, then you'll need to just use the global, as nothing will get passed into your require/define functions.)



Here is a simple example usage of shim to load Backbone. It also adds an export for underscore, even though it doesn't have any dependencies.



require.config({
shim: {
underscore: {
exports: '_'
},
backbone: {
deps: [underscore, jquery],
exports: Backbone
}
}
});

//the main function to bootstrap your code
require(['jquery', 'underscore', 'backbone'], function ($, _, Backbone) { // or, you could use these deps in a separate module using define

});


Note: this simplified code assumes that jquery, backbone and underscore are in files named jquery.js, backbone.js and underscore.js in the same directory as this main code (which becomes the baseURL for require). If this isn't the case, you'll need to use a paths config.



I personally think with the built-in shim functionality, the advantages of not using a forked version of Backbone & Underscore outweigh the benefits of using the AMD fork recommended in the other popular answer, but either way works.


[#89122] Sunday, November 13, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ignacio

Total Points: 467
Total Questions: 128
Total Answers: 79

Location: Luxembourg
Member since Tue, Mar 14, 2023
1 Year ago
ignacio questions
;