[Pkg-javascript-commits] [backbone] 11/37: inject js library with `Backbone.use(myLib)`

Jonas Smedegaard js at moszumanska.debian.org
Sat May 3 17:02:46 UTC 2014


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag 0.9.1
in repository backbone.

commit 701350ab904bc3002e448ffeadeb2d87f1fc4cb0
Author: Matt Smith <matthewgarysmith at gmail.com>
Date:   Sun Sep 11 10:17:28 2011 -0400

    inject js library with `Backbone.use(myLib)`
---
 backbone.js    | 19 +++++++++++++++++++
 test/test.html |  1 +
 test/use.js    | 26 ++++++++++++++++++++++++++
 3 files changed, 46 insertions(+)

diff --git a/backbone.js b/backbone.js
index d72fa9c..7fe5c03 100644
--- a/backbone.js
+++ b/backbone.js
@@ -40,6 +40,25 @@
   // For Backbone's purposes, jQuery, Zepto, or Ender owns the `$` variable.
   var $ = root.jQuery || root.Zepto || root.ender;
 
+
+  // 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 `use()`
+  // 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.use(jQuery)
+  //
+  Backbone.use = 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() {
diff --git a/test/test.html b/test/test.html
index eb7fa27..68181bd 100644
--- a/test/test.html
+++ b/test/test.html
@@ -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="use.js"></script>
 </head>
 <body>
   <h1 id="qunit-header">Backbone Test Suite</h1>
diff --git a/test/use.js b/test/use.js
new file mode 100644
index 0000000..e88ca22
--- /dev/null
+++ b/test/use.js
@@ -0,0 +1,26 @@
+$(document).ready(function() {
+
+  var view = new Backbone.View({
+    id        : 'test-use',
+    className : 'test-use'
+  });
+
+  // a mock javascript library
+  var myLib = function(){ return "spam" }
+
+  module("Backbone.use");
+
+  test('Backbone.use', function() {
+    view.el = document.body;
+
+    // switch to mock library and see if it is being used
+    Backbone.use(myLib);
+    ok(view.$('#qunit-header a') === 'spam');
+
+    // switch back to jQuery and make sure it works
+    Backbone.use(jQuery);
+    ok(view.$('#qunit-header a').get(0).innerHTML.match(/Backbone Test Suite/));
+
+  });
+
+});
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/backbone.git



More information about the Pkg-javascript-commits mailing list