我想加載Backbone和Underscore(以及jQuery)與RequireJS。使用Backbone和Underscore的最新版本,似乎有點棘手。首先,Underscore自動將自己註冊爲一個模塊,但Backbone假定Underscore在全球範圍內可用。我還應該注意到Backbone似乎並沒有將自己註冊爲一個與其他庫不一致的模塊。這是我能想出的作品最好的main.js:使用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();
})
});
我應該指出的是,雖然它的工作原理,優化扼流圈就可以了。我收到以下內容:
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
有沒有更好的處理方法?謝謝!
你使用任何教程做它嗎? – kaha
我瀏覽了http://backbonetutorials.com/organizing-backbone-using-modules/等各種教程,但它們似乎已經過時了最新版本的下劃線和主幹。 – Aaronius
我還發現requirejs很難與其他庫一起使用,反之亦然。這就是爲什麼我創建了一個使用起來更容易,並且使用角度進行測試的庫。底部有一個演示應用程序:http://gngeorgiev.github.io/Modulerr.js/您也可以將所有腳本組合成一個不依賴於Modulerr.js –