Sto provando a caricare Backbone e Underscore (oltre a jQuery) con RequireJS. Con le ultime versioni di Backbone e Underscore, sembra un po 'complicato. Per uno, Underscore si registra automaticamente come un modulo, ma Backbone presuppone che Underscore sia disponibile globalmente. Dovrei anche notare che Backbone non sembra registrarsi come un modulo che lo rende un po 'incoerente con le altre librerie. Questo è il miglior main.js ho potuto venire con che funziona:Caricamento Backbone e Underscore utilizzando RequireJS
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();
})
});
Devo dire che, mentre si lavora, l'ottimizzatore soffoca su di esso. Ricevo quanto segue:
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
C'è un modo migliore di gestirlo? Grazie!
Lo hai fatto utilizzando un tutorial? – kaha
Ho esaminato varie esercitazioni come http://backbonetutorials.com/organizing-backbone-using-modules/ ma sembrano ormai obsolete con le ultime versioni di underscore e backbone. – Aaronius
Ho trovato anche requirejs difficile da usare con altre librerie e viceversa. Questo è il motivo per cui ho creato una libreria che è molto più facile da usare ed è testata con angolari. C'è una demo application in basso: http://gngeorgiev.github.io/Modulerr.js/ Puoi anche combinare tutti gli script in uno senza la dipendenza da Modulerr.js –