diff --git a/backbone.js b/backbone.js index e0c5fed1..ed88febc 100644 --- a/backbone.js +++ b/backbone.js @@ -9,25 +9,38 @@ // 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 = this.Backbone = {}; + Backbone = root.Backbone = {}; } // Current version of the library. Keep in sync with `package.json`. Backbone.VERSION = '0.3.3'; // Require Underscore, if we're on the server, and it's not already present. - var _ = this._; + var _ = root._; if (!_ && (typeof require !== 'undefined')) _ = require('underscore')._; // For Backbone's purposes, either jQuery or Zepto owns the `$` variable. - var $ = this.jQuery || this.Zepto; + var $ = root.jQuery || root.Zepto; + // 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; + }; + // Turn on `emulateHTTP` to use support legacy HTTP servers. Setting this option will // fake `"PUT"` and `"DELETE"` requests via the `_method` parameter and set a // `X-Http-Method-Override` header. diff --git a/test/noconflict.js b/test/noconflict.js new file mode 100644 index 00000000..6122f975 --- /dev/null +++ b/test/noconflict.js @@ -0,0 +1,12 @@ +$(document).ready(function() { + + module("Backbone.noConflict"); + + test('Backbone.noConflict', function() { + var noconflictBackbone = Backbone.noConflict(); + equals(window.Backbone, undefined, 'Returned window.Backbone'); + window.Backbone = noconflictBackbone; + equals(window.Backbone, noconflictBackbone, 'Backbone is still pointing to the original Backbone'); + }); + +}); \ No newline at end of file diff --git a/test/test.html b/test/test.html index d350ad2a..46dbad03 100644 --- a/test/test.html +++ b/test/test.html @@ -10,6 +10,7 @@ +