我正在浏览下面从http://backbonejs.org/backbone.js
复制的Backbone.js源代码(function(root, factory) {
// Set up Backbone appropriately for the environment. Start with AMD.
if (typeof define === 'function' && define.amd) {
define(['underscore', 'jquery', 'exports'], function(_, $, exports) {
// Export global even in AMD case in case this script is loaded with
// others that may still expect a global Backbone.
root.Backbone = factory(root, exports, _, $);
});
// Next for Node.js or CommonJS. jQuery may not be needed as a module.
} else if (typeof exports !== 'undefined') {
var _ = require('underscore');
factory(root, exports, _);
// Finally, as a browser global.
} else {
root.Backbone = factory(root, {}, root._, (root.jQuery || root.Zepto || root.ender || root.$));
}
}(this, function(root, Backbone, _, $) {
// Initial Setup
// -------------
// Save the previous value of the `Backbone` variable, so that it can be
// restored later on, if `noConflict` is used.
var previousBackbone = root.Backbone;
// Create local references to array methods we'll want to use later.
var array = [];
var push = array.push;
var slice = array.slice;
var splice = array.splice;
// Current version of the library. Keep in sync with `package.json`.
Backbone.VERSION = '1.1.2';
// For Backbone's purposes, jQuery, Zepto, Ender, or My Library (kidding) owns
// the `$` variable.
Backbone.$ = $;
在CommonJS / node.js配置中,它显示Next for Node.js or CommonJS. jQuery may not be needed as a module.
并使用jquery的第四个factory(root, exports, _)
参数调用$
作为未定义。在工厂函数中,它Backbone.$ = $
,因此Backbone.$
将是未定义的?那么不会有很多东西(AJAX,$el
包装等)破坏?
答案 0 :(得分:2)
使用jQuery的主要原因是使用DOM操作,这在后端(nodejs)中是无用的,因此不需要将其作为依赖项加载。要解决此问题,您可以创建一个使用jQuery返回BackboneJS的简单模块。
// libs/backbone.js
var backbone = require("backbone");
backbone.$ = require("jquery");
module.exports = backbone;