Chapter 4
=========
Intro to Backbone.js
====================
> *Code is not an asset. It's a liability. The more you write, the more
> you'll have to maintain later.*
>
> — Unknown
This chapter will demonstrate:
- Setting up a Backbone.js app from scratch and installing
dependencies
- Working with Backbone.js collections
- Backbone.js event binding
- Backbone.js views and subviews with Underscore.js
- Refactoring Backbone.js code
- AMD and Require.js for Backbone.js development
- Require.js for Backbone.js production
- A simple Backbone.js starter kit
Backbone.js has been around for a while so it's very mature and can be
trusted to be used in serious front-end development projects. This
framework is decidedly minimalistic and unopinionated. You can use
Backbone.js with a lot of other libraries and modules. I think of
Backbone.js as the foundation to build a custom framework that will be
tightly suited to your particular use case.
Some people are turned off by the fact that Backbone.js is unopinionated
and minimalistic. They prefer frameworks that do more for them and
enforce a particular way of doing things (e.g., [the Angular best
practices](https://github.com/johnpapa/angular-styleguide)
(https://github.com/johnpapa/angular-styleguide)). This is totally fine
with me, and you can pursue the study of a more complex front-end
framework. They all fit nicely into the Node.js stack and the ecosystem.
For the purpose of this book, Backbone.js is ideal because it provides
some much needed sanity to the plain nonframework jQuery code, and at
the same time it doesn't have a steep learning curve. All you need to
know is a few classes and methods, which we cover in this book.
Everything else is JavaScript, not a domain-specific language.
Setting Up Backbone.js App from Scratch
=======================================
We're going to build a
typical starter Hello World application using Backbone.js and
Mode-View-Controller (MVC) architecture. It might sound like overkill in
the beginning, but as we go along we'll add more and more complexity,
including models, subviews, and collections.
Full source code for the Hello World app is available under
`05``-backbone/hello-world` and on
[GitHub](https://github.com/azat-co/fullstack-javascript/tree/master/05-backbone/hello-world)
(https://github.com/azat-co/fullstack-javascript/tree/master/05-backbone/hello-world).
Backbone.js Dependencies
------------------------
Supplemental
video which walks you through the implementation and demonstrates the
project: .
Download the following libraries:
- jQuery v2.1.4 development source file
- Underscore.js v1.8.3 development source file
- Backbone.js v1.2.3 development source file
Obviously by the time this book is in print, these versions won't be the most recent. I recommend sticking with the versions in this book, because that's what I used to test all the examples and projects. Using different, newer versions might cause some unexpected conflicts.
Create an `index.html` file, and include these frameworks in this file
like this:
We can also put `