Front End Developer with Interest in Data Visualization
Introduction to Backbone
Your probably wondering, why do we have to write so much code for displaying HTML elements that don't currently have a feature rich component. These are called single page applications. Backbone consists of building web pages using the following architectures:
Models
Views
Collections
Routers
In order to undersand the ease with which Backbone lets you render your page, you have to have built a large project using jQuery that eventually becomes difficult to manage. Backbone allows developers to write feature rich code that can similarly be written in jQuery in addition to keeping your code organized.
I personally took a long time to understand the use of Backbone for Single Page Application due to its structure. It's specially helpful when your using underscore on a regular basis. jQuery can provide you with the same feature rich functionality, however as the project becomes larger it can soon become sluggish.
Refer to the following links to guide you through a beginners introduction to Backbone.
I'm going to start of with a simple code block for a Backbone Model with a class Person.
Step 2
A View allows you to listen to events from the DOM and also render events to the DOM. In this case we want to render the attributes of the instance of Person to the DOM.
Step 3
The code simply above will not render anything on the DOM, since we have not called the render method. We need to create an instance of Person and an instance of PersonView and append the data from Person to the DOM using the PersonView instance.
Notice how person and personView have been instantiated. Instance personView is of type PersonView that takes in the Backbone Model person.
Step 4
Now lets talk about collections, a collection is a filtered list of people that you would like to view on the DOM, from your data model. In case you invoke a filter function that only wants to see the people older than 21 you would want to refer to that list as a collection that is rendered to the DOM using a Backbone Collection View.
In the example above, we have made one person and rendered them on to the DOM, in the case that we want to put a list of people on the DOM, we can create a Collection which is essentially an Array of models and render them on the DOM. We will also need to create a View for the Collection and invoke the method for rendering the list of people on the DOM. The list could be the entire list of people or a subset of the list.
Step 5
Now lets create a View for the Collection and render the list of People on the DOM. In order to stay modular, you will notice, the data does not change a lot, however, we may have several Views, using the same data, but manupilating different parts of the DOM. Essentially, you have a single model, and single collection but several views, rendering different parts of the DOM.
Step 6
Use the Chrome Console to navigate the attributes of Person, PersonView and PeopleCollection. Again, an instance of the PeopleView has not been created, and the render function has also not been invoked, therefore this code above alone will not run. Use the HTML code below or create instances in the Chrome Developer's console to invoke the render method on JSON data.