Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

…ackbone.
  • Loading branch information...
commit 67d61fc355b00fcbd402ca551799138441dcf764 1 parent da52ae1
@samuelclay samuelclay authored
Showing with 29 additions and 3 deletions.
  1. +16 −3 backbone.js
  2. +12 −0 test/noconflict.js
  3. +1 −0  test/test.html
View
19 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.
View
12 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');
+ });
+
+});
View
1  test/test.html
@@ -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>
Please sign in to comment.
Something went wrong with that request. Please try again.