Skip to content

Commit

Permalink
merging in setDomLibrary
Browse files Browse the repository at this point in the history
  • Loading branch information
jashkenas committed Feb 2, 2012
2 parents 8a427f3 + e97c58f commit 0ffd7ea
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
19 changes: 19 additions & 0 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@
// For Backbone's purposes, jQuery, Zepto, or Ender owns the `$` variable.
var $ = root.jQuery || root.Zepto || root.ender || root.$;


// Set the javascript library that will be used for the selector work
// (a.k.a. the `$` variable).
//
// By default Backbone will use: jQuery, Zepto, or Ender; but the `setDomLibrary()`
// method let's you inject an alternate javascript library (or a mock
// library for testing your views outside of a browser). This is also useful
// if you are using a packaging library -- to add support for `require()`
// calls in the browser (e.g. ender or browserify). The scoping used to
// support `require()`, prevents Backbone from automatically loading the
// default javascript library.
//
// Backbone.setDomLibrary(jQuery)
//
Backbone.setDomLibrary = function(lib) {
$ = lib;
};


// Runs Backbone.js in *noConflict* mode, returning the `Backbone` variable
// to its previous owner. Returns a reference to this Backbone object.
Backbone.noConflict = function() {
Expand Down
30 changes: 30 additions & 0 deletions test/setdomlibrary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
$(document).ready(function() {

// a mock object that looks sufficiently jQuery-ish
var myLib = function() {
return {
attr: function() { return "spam" },
html: function() { return "spam" },
hasClass: function() { return "spam" }
};
};

var viewAttrs = { id: 'test-setdomlibrary', className: 'test-setdomlibrary' }

module("Backbone.setDomLibrary");

test('Changing jQuery library to custom library', function() {
Backbone.setDomLibrary(myLib);
var view = new Backbone.View(viewAttrs);

ok(view.$el.hasClass('test-setdomlibrary') === 'spam');
});

test('Changing jQuery library back to global jQuery', function() {
Backbone.setDomLibrary(jQuery);
var view = new Backbone.View(viewAttrs);

ok(view.$el.hasClass('test-setdomlibrary'));
});

});
1 change: 1 addition & 0 deletions test/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<script type="text/javascript" src="view.js"></script>
<script type="text/javascript" src="sync.js"></script>
<script type="text/javascript" src="speed.js"></script>
<script type="text/javascript" src="setdomlibrary.js"></script>
</head>
<body>
<h1 id="qunit-header">Backbone Test Suite</h1>
Expand Down

0 comments on commit 0ffd7ea

Please sign in to comment.