Skip to content

Commit

Permalink
UMD wrapper for AMD, node and browser globals.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrburke committed Nov 7, 2013
1 parent 41e4703 commit ea29bd9
Show file tree
Hide file tree
Showing 3 changed files with 2,133 additions and 17 deletions.
37 changes: 20 additions & 17 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,38 @@
// For all details and documentation:
// http://documentcloud.github.com/backbone

(function(){
(function(root, factory) {
// Set up Backbone appropriately for the environment.
if (typeof define === 'function' && define.amd) {
// AMD
define(['underscore', 'jquery', 'exports'], function(_, $, exports) {

This comment has been minimized.

Copy link
@juhovh

juhovh Nov 14, 2013

What is the 'exports' dependency used for? Why wouldn't a simple {} as in the browser global work?

// 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, _, $);
});
} else if (typeof exports !== 'undefined') {
// Node/CommonJS, no need for jQuery in that case.
factory(root, exports, require('underscore'));
} else {
// Browser globals
// For Backbone's purposes, jQuery, Zepto, or Ender owns the `$` variable.
root.Backbone = factory(root, {}, root._, (root.jQuery || root.Zepto || root.ender || root.$));
}
}(this, function(root, Backbone, _, $) {

// Initial Setup
// -------------

// Save a reference to the global object.
var root = this;

// Save the previous value of the `Backbone` variable.
var previousBackbone = root.Backbone;

// The top-level namespace. All public Backbone classes and modules will
// be attached to this. Exported for both CommonJS and the browser.
var Backbone;
if (typeof exports !== 'undefined') {
Backbone = exports;
} else {
Backbone = root.Backbone = {};
}

// Current version of the library. Keep in sync with `package.json`.
Backbone.VERSION = '0.5.3';

// Require Underscore, if we're on the server, and it's not already present.
var _ = root._;
if (!_ && (typeof require !== 'undefined')) _ = require('underscore')._;

This comment has been minimized.

Copy link
@juhovh

juhovh Nov 14, 2013

Why is this not removed, shouldn't the setup above already take care of initializing the _ variable?


// For Backbone's purposes, jQuery, Zepto, or Ender owns the `$` variable.
var $ = root.jQuery || root.Zepto || root.ender;

// Runs Backbone.js in *noConflict* mode, returning the `Backbone` variable
// to its previous owner. Returns a reference to this Backbone object.
Backbone.noConflict = function() {
Expand Down Expand Up @@ -1161,4 +1163,5 @@
return string.replace(/&(?!\w+;|#\d+;|#x[\da-f]+;)/gi, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#x27;').replace(/\//g,'&#x2F;');
};

}).call(this);
return Backbone;
}));
59 changes: 59 additions & 0 deletions test/test-amd.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<!DOCTYPE HTML>
<html>
<head>
<title>Backbone Test Suite</title>
<link rel="stylesheet" href="vendor/qunit.css" type="text/css" media="screen" />
<script type="text/javascript" src="vendor/json2.js"></script>
<script type="text/javascript" src="vendor/qunit.js"></script>
<script type="text/javascript" src="vendor/jslitmus.js"></script>
<script type="text/javascript" src="vendor/require.js"></script>
<script>
// Tests are loaded async, so wait for them
// to load before starting.
QUnit.config.autostart = false;

require.config({
paths: {
'jquery': 'vendor/jquery-1.5',
'underscore': 'vendor/underscore-1.1.6',
'backbone': '../backbone'
},
// jQuery/underscore are old, so shim.
shim: {
jquery: {
exports: 'jQuery'
},
underscore: {
exports: '_'
}
}
});

var root = this;

require(['backbone'], function(Backbone) {
require([
'noconflict',
'events',
'model',
'collection',
'router',
'view',
'sync',
'speed',
], function() {
QUnit.start();
});
});
</script>
</head>
<body>
<h1 id="qunit-header">Backbone Test Suite</h1>
<h2 id="qunit-banner"></h2>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<br /><br />
<h1 id="qunit-header"><a href="#">Backbone Speed Suite</a></h1>
<div id="jslitmus_container" style="margin: 20px 10px;"></div>
</body>
</html>
Loading

0 comments on commit ea29bd9

Please sign in to comment.