Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 64 additions & 18 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,38 @@
// For all details and documentation:
// http://documentcloud.github.com/backbone

(function(){
// Use a factory function to attach Backbone properties to an exports value.
// Code at the end of this file creates the right define, require and exports
// values to allow Backbone to run in a CommonJS or AMD container or in the
// default browser environment.
(function(root, define){ define('backbone', function(require, exports) {

// 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 = {};
}
// be attached to this.
var Backbone = exports;

// 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')._;
// Require Underscore.
var _ = require('underscore');

// For Backbone's purposes, jQuery, Zepto, or Ender owns the `$` variable.
var $ = root.jQuery || root.Zepto || root.ender;
// The DOM/Ajax library used internally by Backbone. It can be set to any
// library that supports the portions of the jQuery API used by Backbone.
// In the browser, by default Backbone looks for a global jQuery, Zepto or Ender.
var $ = require('jquery');

// Runs Backbone.js in *noConflict* mode, returning the `Backbone` variable
// to its previous owner. Returns a reference to this Backbone object.
Backbone.noConflict = function() {
root.Backbone = previousBackbone;
return this;
return exports;
};

// Turn on `emulateHTTP` to support legacy HTTP servers. Setting this option will
Expand Down Expand Up @@ -1170,4 +1167,53 @@
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);
})}).call(this, this, typeof define === 'function' && define.amd ? define : function (id, factory) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any way to break out this define function? putting it inline seems weird to me.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tbranyen, here is alternate path that could be taken for the patch. It still means passing a function into an anonymous, immediately executed function, but it may be aesthetically pleasing:

    (function (factory){
      var root = this,
          previousBackbone = root.Backbone;

      if (typeof exports !== 'undefined') {
        // CommonJS
        var $;
        try {
          $ = require('jquery');
        } catch (e) {
          // ignore, it is ok in node if it fails.
        }
        factory(root, previousBackbone, exports, require('underscore'), $);
      } else if (typeof define === 'function' && define.amd) {
        // AMD
        define('backbone', ['underscore', 'jquery', 'exports'], function (_, $, exports) {
          factory(root, previousBackbone, exports, _, $);
        });
      } else {
        // Browser globals
        factory(root, previousBackbone, (root.Backbone = {}), root._, (root.jQuery || root.Zepto || root.ender));
      }
    }(function (root, previousBackbone, Backbone, _, $) {
      //Backbone code goes in here
    });

If you would rather see this approach taken, I'll do a different branch with this approach today.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went ahead and did a branch that uses this different registration approach, you can do the compare here. It passes the tests, including the simple node one and the requirejs-based one I have in a test branch.

If you prefer I do a pull with this new optamd2 branch instead of this pull request, then I will close out this pull request and open another with that branch.


if (typeof exports !== 'undefined') {
// CommonJS has require and exports, use them and execute
// the factory function immediately. Provide a wrapper
// for require to deal with jQuery.
factory(function(id) {
// jQuery most likely cannot be loaded
// in a CommonJS environment, unless the developer
// also uses a browser shim like jsdom. Allow
// for that possibility, but do not blow
// up if it does not work. Use of a
// try/catch has precedent in Node modules
// for this kind of situation.
try {
return require(id);
} catch (e) {
// Do not bother returning a value, just absorb
// the error, the caller will receive undefined
// for the value.
}
}, exports);
} else {
// Plain browser. Grab the global.
var root = this;

// Create an object to hold the exported properties for Backbone.
// Do not use "exports" for the variable name, since var hoisting
// means it will shadow CommonJS exports in that environmetn.
var exportValue = {};

// Create a global for Backbone.
// Call the factory function to attach the Backbone
// properties to the exports value.
factory(function(id) {
if (id === 'jquery') {
// Support libraries that support the portions of
// the jQuery API used by Backbone.
return root.jQuery || root.Zepto || root.ender;
} else {
// Only other dependency is underscore.
return root._;
}
}, exportValue);

// Create the global only after running the factory,
// so that the previousBackbone for noConflict is found correctly.
root.Backbone = exportValue;
}
});