Backbone.js and jQuery

Backbone is said to handle all higher-level abstractions, while jQuery or similar libraries work with the DOM, normalize events, etc.

Can someone please help me understand this statement with any simple practical example.

Also, one important feature of the MVC structure, such as Backbone, Knockout, is that it keeps the model (data) and view in sync. But this, apparently, is characteristic for the page level, and not for the entire application. So, can we have a model / data and view synchronized across multiple pages. (View of global)

+79
javascript jquery javascript-framework
Mar 15 '12 at 7:07
source share
4 answers

Backbone / Knockout is commonly used for single page applications. Therefore, when jQuery is a toolkit that can be used with any web page, Backbone is designed for a specific type of application and helps you organize your own code for it. At least in my experience, one of the biggest problems when creating a one-page application is that the code is clean and modular, and the foundation helps.

Characteristics of a typical base application:

  • Essentially a static html page where nothing is generated on the server
  • The server acts as a json REST api that provides content for the application
  • Dom elements for displaying data are created using javascript in database views using jQuery and various template libraries to help
  • Communication with the server, as well as between the various parts of the application, is carried out through trunk models.

Regarding your question about saving data synchronized across multiple pages, my instinctive answer is that you do not need multiple pages: the user can understand that the page is changing, the address in the title bar is changing thanks to the pushState function, but technically the whole application is one page.

The biggest advantages of this approach are a smooth user interface (without page reloading), good caching support, since everything except json data is static content, since mobile targets allow you to turn a web application into a mobile application using phoneGap (because everything except json, is static).

+53
Mar 15 2018-12-15T00:
source share

Your introductory sentence was actually a very good statement about the differences between Backbone.js and jQuery, so let's unpack it a bit.

On the one hand, the two libraries do not compete at all - they are free.

As an example, here are some things I would do with jQuery:

  • Animated Slideshows
  • Shape control enhancements, such as iOS spinner number
  • Toggle element visibility based on class name

And some things that I could do in Backbone.js:

  • Create a photo album where the user clicks on a thumbnail and can view a larger version of the photo, as well as some data, such as the camera used, location and name of the photographer.
  • Create a master page / part type that represents the data grid and allows the user to click on individual elements and update them in the form.

jQuery stands out at the micro level - selecting page elements, smoothing out differences in how browsers handle events.

Backbone.js is larger. It helps you manage the logic of data and applications. In the above example of a photo album, Backbone provides several useful structures: you have something to contain all the data associated with the photos (model), a list of all the photos in the album (collection), and somewhere you put the logic that determines what happens when the user clicks on a thumbnail (view). These are the basic controls for a database or application.

Backbone.js benefits from jQuery, though, or something like that to help display the results of your data and application logic in the DOM. For example, you can use jQuery to select an element on a page that will serve as a container for your Backbone application. Also commonly used is jQuery $(function () {}); to launch parts of your backbone control. You will probably also see error messages checking form fields using jQuery.

In jQuery, you can create large, complex user interfaces. We have several applications that I support at work. But it’s difficult to work with them, since jQuery is not intended to provide the structure of the application. In particular, the jQuery API, based on the selection of groups of elements and then passing callback functions that control these elements, is not a good template for use in a large complex control or application. You have many nested functions, and it’s very difficult to understand what is going on.

I am currently recycling one of these controls in Backbone.js. As a final example, here is a brief description of how my thought process differs when working with the same control in both different libraries.

In jQuery, it bothers me:

  • Am I using the right selector to capture a group of li elements that I want?
  • Do I need to re-populate this list of values ​​when this Ajax call ends?
  • How can I put these array values ​​back into the input elements on the page?

In Backbone, I'm more focused on:

  • What is the correct logic for checking this set of properties of my model element?
  • When the user clicks the “Add” button, should I immediately add a new item to the collection or do I need to wait until they fill all the data, and is this “really”?
  • How should an item in my collection respond when an item is immediately before or after it is deleted?

jQuery handles detailed details, while Backbone handles higher-level ones.

In conclusion, note that I used the words "control" and "app" when discussing Backbone.js examples. It is not true that Backbone.js is for single-page applications only. It is true, however, that Backbone.js is good for creating complex applications that manage data and handle a lot of logic. It would be foolish to use it for small user interface elements - the additional structure that it imposes is not required.

Update: Regarding multiple pages, yes, Backbone really provides a powerful mechanism for saving your data. Each model has a save method that will make an AJAX call to store changes on the server. Thus, while you save your data, you can use a multi-page application. This is a very flexible model, and how we are likely to end using Backbone at work. Although I would like to create a one-page application, we have 10 years of experience in our existing multi-page application. We aim to rebuild some of our more intensive user interface components in Backbone, and then synchronize the changes with the server before the user moves to another page.

+127
Mar 16 '12 at 1:30
source share

I have never heard of people using backbone.js on multiple pages. This is almost always some kind of one-page application.

One page can have many different models, views, and states and can lead to a full-blown powerful application.

If you already have a server template / rendering view in java, then backbone.js is NOT for you. In order to get the most out of backbone.js, you have to move or duplicate some of this code in the front panel javascript.

If you do not want to make a one-page application (this means that the application is without page refresh or changes, but the URL can still change and may look like several pages for the user), you can save all your MVC to the server, and you don’t need backbone network.

Edit:

What is the spine - is moving some MVC materials that are usually processed on the server, and moving them to the client. For many, this means forgetting about the server and just writing the application as a one-page javascript application. The server becomes only a JSON / REST data source. If you are not ready to do this, then backbone.js is not so useful.

+3
Mar 16 '12 at 4:22
source share

Backbone is the MV * framework, and jQuery is the DOM toolkit.

The main features of the MV * application are routing, data binding, templates / views, models, and data access. The backbone may be partially dependent on jQuery.

jQuery is a robust DOM request API with extensive browser support and an active community. It comes with event handling, deferred objects, and animations.

Binding a simple event using jQuery

 // When any <p> tag is clicked, we expect to see '<p> was clicked' in the console. $( "p" ).on( "click", function() { console.log( "<p> was clicked" ); }); 
+2
Mar 16 2018-12-16T00:
source share



All Articles