Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Backbone.js have a hard dependency on jQuery?

Tags:

According to backbone js website:

Backbone's only hard dependency is Underscore.js ( > 1.3.1). For RESTful persistence, history support via Backbone.Router and DOM manipulation with Backbone.View, include json2.js, and either jQuery (1.4.2) or Zepto.

I tested with code below, removing jQuery and the Backbone view throws an error.

<html> <head>     <title>asda</title>       <!--<script src="http://code.jquery.com/jquery-1.7.2.min.js"></script>-->     <script src="http://documentcloud.github.com/underscore/underscore.js"></script>     <script src="http://documentcloud.github.com/backbone/backbone.js"></script>                 <script type="text/javascript" charset="utf-8" async defer>         SearchView = Backbone.View.extend({             initialize: function(){                 alert("Alerts suck.");             }         });          // The initialize function is always called when instantiating a Backbone View.         // Consider it the constructor of the class.         var search_view = new SearchView;     </script>    </head> <body>  </body> </html> 

How can Backbone.View and Backbone.Router work without jQuery?

like image 682
TonyTakeshi Avatar asked Apr 20 '12 07:04

TonyTakeshi


People also ask

Does backbone use jQuery?

By default Backbone will use: jQuery, // Zepto, or Ender; but the `setDomLibrary()` method lets you inject an // alternate JavaScript library (or a mock library for testing your views // outside of a browser).

Is Backbone JS still relevant?

Backbone. Backbone has been around for a long time, but it's still under steady and regular development. It's a good choice if you want a flexible JavaScript framework with a simple model for representing data and getting it into views.

Is Backbone JS frontend or backend?

Backbone. js is a lightweight JavaScript library for building the structure of web applications. It's often used on the front end, but it can be used on the back end as well. The main aspect of Backbone that makes it suitable for use in non-browser environments is its lack of dependence on DOM manipulation or jQuery.

Is Backbone is a framework of JavaScript?

js is a JavaScript rich-client web app framework based on the model–view–controller design paradigm, intended to connect to an API over a RESTful JSON interface. Backbone is known for being lightweight, as its only hard dependency is on one JavaScript library, Underscore.


2 Answers

While Backbone.View does depend on a DOM manipulation library, ie you can't use vanilla javascript that is not organized in to a library of some sort, you can configure Backbone to use whatever library you'd like.

See the following from the Backbone source:

// Set the JavaScript library that will be used for DOM manipulation and // Ajax calls (a.k.a. the `$` variable). By default Backbone will use: jQuery, // Zepto, or Ender; but the `setDomLibrary()` method lets you inject an // alternate JavaScript library (or a mock library for testing your views // outside of a browser). Backbone.setDomLibrary = function(lib) {   $ = lib; }; 

Calling this method will allow you to use whatever library you want.

For example:

Backbone.setDomLibrary(myCustomLibrary); 
like image 116
Jeffrey K Avatar answered Oct 11 '22 07:10

Jeffrey K


You can use the Backbone.Model without jQuery, but Backbone.View will require either jQuery or Zepto, just like the docs state.

like image 33
Jimmy Avatar answered Oct 11 '22 09:10

Jimmy