Adding Backbone.noConflict() and keeping track of the original root.Backbone.

This commit is contained in:
Samuel Clay
2011-03-23 14:08:37 -04:00
parent da52ae1c43
commit 67d61fc355
3 changed files with 29 additions and 3 deletions

View File

@@ -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.

12
test/noconflict.js Normal file
View File

@@ -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');
});
});

View File

@@ -10,6 +10,7 @@
<script type="text/javascript" src="vendor/underscore-1.1.5.js"></script>
<script type="text/javascript" src="../backbone.js"></script>
<script type="text/javascript" src="noconflict.js"></script>
<script type="text/javascript" src="events.js"></script>
<script type="text/javascript" src="model.js"></script>
<script type="text/javascript" src="collection.js"></script>