正如@ChristiMihai 提到的,Backbone 创建了一个全局 Backbone 对象,正确。让我举一个例子,说明我在 Require.js / Backbone / Handlebars 应用程序中所做的事情:
首先,我在 Require config 中包含<head>
:
var require_config = {
baseUrl: "/javascripts",
waitSeconds: 5,
paths: {
'cdnjs': 'http://ajax.cdnjs.com/ajax/libs',
'aspnetcdn': 'http://ajax.aspnetcdn.com/ajax',
'cloudflare': 'http://cdnjs.cloudflare.com/ajax/libs',
'local': '/javascripts'
}
}
if (typeof require !== 'undefined') {
require.config(require_config);
} else {
var require = require_config;
}
之后,我启动了一个 require 模块,例如:
define([
'app'
],
function() {
console.log('Homepage module');
/*
... this is the meat of your app...
you can add other dependencies beside `app` too
*/
});
现在app
是通过 baseUrl 解析的主要依赖项,/javascripts/app.js
并按顺序包含所有必要的 deps,如下所示:
define([
'order!cdnjs/json2/20110223/json2',
'order!cloudflare/underscore.js/1.3.1/underscore-min',
'order!cloudflare/backbone.js/0.9.2/backbone-min',
'order!handlebars/handlebars-1.0.0.beta.6.min',
'order!lib/ns',
'bootstrap'
], function(){});