This tutorial builds on top of the first part and continue with BacboneJS’s Models, Collections and Views.
BackboneJS Tutorial series:
- Backbone.js for Absolute Beginners - Getting started (Part 1: Intro)
- Backbone.js for absolute beginners - getting started (part 2: Models, Collections and Views) 👈 you are here
- Backbone.js for absolute beginners - getting started (part 3: CRUD)
- Backbone.js for absolute beginners - getting started (part 4: Routers)
Todo App in Backbone
After completing this example app, you will have experience and a basic understanding of Backbone modules!
(Updated: 2013-02-02, 2013-11-24) Notice: This tutorial was written using Backbone v.0.9.x, now version 1.1.x or later are out. However, all the principles explained here apply to both.
Todo app Boilerplate
Let’s start again with the initial HTML file used on 1.1. Now, instead of div#container let’s add the following HTML code:
1 |
|
We will implement a Todo list, basically an unordered list (ul) of elements with checkboxes.
Backbone.Model
Models are the heart of every application. It contains the interactive data and the logic surrounding it, such as data validation, getters and setters, default values, data initialization, conversions, etc.
For our example, we are going to create a model called Todo
, which will store a string of text (title) and whether the task has been completed or not.
1 |
|
Notice that by convention, class names are capitalized, while instance variables and objects are not. Another important aspect of models is that their properties are dynamic; they can be created on the fly and don’t have any specific types associated with them.
Test what you just coded!
After you completed the code snippet above you can open your browser console (chrome’s console: ctrl+shift+i -or- ⌘+alt+i) and try this out, to get familiar with the models:
1 | var todo = new app.Todo({title: 'Learn Backbone.js', completed: false}); // create object with the attributes specified. |
Backbone.Collection
As its name indicates, collections are ordered sets of models. You can get and set models in the collection, listen for events when any element in the array changes, and fetch for model’s data from the server.
E.g.: todoList.fetch();
Collections allow to save data (in database, file, memory), and it requires a reference to it. Therefore, you need to specify the url
parameter with a relative URL, where the model’s resource would be located on the server. Otherwise, you will get errors like:
A "url" property or function must be specified
We are not going to use a backend server for simplicity (I will do a new post for that); instead, we will use HTML5’s local storage for persistence through a Backbone’s plugin. So, we need to define the localStorage property instead of the URL. You need to include the backbone-localstorage.js with the rest of your libs as shown in the full code:
<script src="http://cdnjs.cloudflare.com/ajax/libs/backbone-localstorage.js/1.0/backbone.localStorage-min.js" type="text/javascript">
1 |
|
Test what you just coded!
(Google’s Chrome console shortcuts: ctrl+shift+i -or- ⌘+alt+i)
1 | var todoList = new app.TodoList() |
Backbone.View
As mentioned in 1.2, Views doesn’t have the HTML markups for our application, but instead (It’s like the controller in MVC frameworks) process data and link it to templates. It finally render HTML based on events or data changes.
Basic Properties
There are four basic properties in a view: el, initialize, render, and events.
We have already seen the first three and will see later the fourth one. Do you remember the Hello World View from 1.2?
1 | var AppView = Backbone.View.extend({ |
view.el
Every view needs to reference a DOM at all times. Therefore, the view will inject content into this element. This is the el
property. this.el
is created from view’s el
,tagName
, className
, id
or attributes
properties. If none of these are specified, then this.el is an empty div
. The view.$el
it’s a cached jQuery object of the view’s element (view.el).
Initialize/constructor
Here you can pass parameters that will be attached to a model, collection, or view.el.
render
This function injects the markup into the elements. Not all views require having a render function. As you are going to see in the sample code, they can call other view’s render functions.
delegated events
Events are written in the following format:
{"<EVENT_TYPE> <ELEMENT_ID>": "<CALLBACK_FUNTION>"}
E.g.
events: {'keypress #new-todo': 'createTodoOnEnter'}
in jQuery it would be something like:
$('#new-todo').keypress(createTodoOnEnter);
Todo View
Now back to our Todo application: We need a view that renders each of the todo model objects into the page. The item-template
and app.TodoView
will generate each todo item.
1 |
|
In the following block of code, we have the Backbone.View which uses the above template (#item-template
) to fill out the title from the model
we pass along.
1 |
|
When we instantiate a Backbone View, it can receive any parameter that we need. In this case, since we call the parameter model
let’s instantiate it with a Backbone Model (e.g. todo):
var view = new app.TodoView({model: todo});
Also, notice that our view uses a tagName: li
instead of just el
from before. This means that the new rendered elements will be wrapped around a <li></li>
Backbone.Events
This module can be mixed with any object and give it the pub/sub (observer pattern) behavior. Events provide a couple of methods from which we will discuss: on
, off
and trigger
. (If this you are familiar with, then in jQuery, they will work the same way + some excellent built-in features)
Subscribing to Events with on
object.on(event, callback, [context])
Also called bind. It binds an object to an event and a callback. When that event it’s triggered, it executes the callback.
E.g.
todoList.on('add', this.addAll, this);
Every time a new item is add
ed to a Backbone.Collection the built-in event add
(docs for add is triggered. In the example above, after the custom event is triggered, the todoList’s callback addAll()
is executed. Also, the current object is passed with this
as a context
.
Events can also be set on arbitrary objects using underscore.js extend
function:
1 | var object = {}, |
App View
Now, we need another view that takes a collection and renders each of the individual items. We are going to call it ‘AppView’. This is a new large chunk of code so read it closely. Please take a look through this code and identify each of the elements.
1 |
|
What’s next?
Continue with the 3rd part and learn how to make CRUD for your models!