Permalink
Browse files

Adding AMD support to Backbone.

  • Loading branch information...
2 parents 0b507f4 + ea29bd9 commit ab5d2eb4c657ed81e80ac815f687e94a2cb55fef @jashkenas committed Nov 7, 2013
Showing with 2,161 additions and 33 deletions.
  1. +24 −16 backbone.js
  2. +55 −0 test/test-amd.html
  3. +2,054 −0 test/vendor/require.js
  4. +28 −17 test/vendor/underscore.js
View
@@ -6,15 +6,30 @@
// For all details and documentation:
// http://backbonejs.org
-(function(){
+(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.
+ } else if (typeof exports !== 'undefined') {
+ factory(root, exports, require('underscore'));
+
+ // 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 a reference to the global object (`window` in the browser, `exports`
- // on the server).
- var root = this;
-
// Save the previous value of the `Backbone` variable, so that it can be
// restored later on, if `noConflict` is used.
var previousBackbone = root.Backbone;
@@ -25,15 +40,6 @@
var slice = array.slice;
var splice = array.splice;
- // The top-level namespace. All public Backbone classes and modules will
- // be attached to this. Exported for both the browser and the server.
- 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 = '1.1.0';
@@ -43,7 +49,7 @@
// For Backbone's purposes, jQuery, Zepto, Ender, or My Library (kidding) owns
// the `$` variable.
- Backbone.$ = root.jQuery || root.Zepto || root.ender || root.$;
+ Backbone.$ = $;
// Runs Backbone.js in *noConflict* mode, returning the `Backbone` variable
// to its previous owner. Returns a reference to this Backbone object.
@@ -1579,4 +1585,6 @@
};
};
-}).call(this);
+ return Backbone;
+
+}));
View
@@ -0,0 +1,55 @@
+<!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',
+ 'underscore': 'vendor/underscore',
+ 'backbone': '../backbone'
+ },
+ // jQuery is old, so shim.
+ shim: {
+ jquery: {
+ exports: 'jQuery'
+ }
+ }
+ });
+
+ var root = this;
+
+ require(['backbone'], function(Backbone) {
+ require([
+ 'noconflict',
+ 'events',
+ 'model',
+ 'collection',
+ 'router',
+ 'view',
+ 'sync'
+ ], 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>
Oops, something went wrong.

0 comments on commit ab5d2eb

Please sign in to comment.