Book Image

Backbone.js Blueprints

By : Andrew Burgess
Book Image

Backbone.js Blueprints

By: Andrew Burgess

Overview of this book

<p>Backbone.js is an open source, JavaScript library that helps you to build sophisticated and structured web apps. It's important to have well-organized frontend code for easy maintenance and extendability. With the Backbone framework, you'll be able to build applications that are a breeze to manage.<br /><br />In this book, you will discover how to build seven complete web applications from scratch. You'll learn how to use all the components of the Backbone framework individually, and how to use them together to create fully featured applications. In addition, you'll also learn how Backbone thinks so you can leverage it to write the most efficient frontend JavaScript code.<br /><br />Through this book, you will learn to write good server-side JavaScript to support your frontend applications. This easy-to-follow guide is packed with projects, code, and solid explanations that will give you the confidence to write your own web applications from scratch.</p>
Table of Contents (14 chapters)
Backbone.js Blueprints
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Creating the model and collection


Let's start with the Event model class, which will go in the models.js file of the public folder. Separating our code into multiple files is a good first step for organization, but we can go one step further. Previously, each of our classes has been referenced by its own global variable. You probably know that this isn't a really wise technique. If we're using other libraries, frameworks, or plugins, we don't want two components to use the same variable name and mess up the works. In this application, we're going to put all our classes safely inside a single global object. So, we start the models.js file with the following code:

window.App = window.App || {};
App.Models = {};

The first line may seem a bit tricky; why not just use var App = {};? Well, the technique I've used here allows us to not worry about the order our files are loaded in the browser. This line first checks to see whether window.App exists. If it does, it assigns it to itself (basically...