Book Image

Backbone.js Cookbook

By : Vadim Mirgorod
Book Image

Backbone.js Cookbook

By: Vadim Mirgorod

Overview of this book

<p>There is no doubt that the superior rendering power of HTML5, thin-to-thick client transition and REST style communication created a new era in web development, replacing the outdated approach based on browser plugin technologies. Backbone.js allows developers to write lightweight, modular, and scalable JavaScript applications.<br /><br />Backbone.js Cookbook contains a series of recipes that provide practical, step-by-step solutions to the problems that may occur during frontend application development using an MVC pattern. You will learn how to build Backbone applications utilizing the power of popular Backbone extensions and integrating your app with different third party libraries. You will also learn how to fulfill the requirements of the most challenging tasks.<br /><br />The first chapter of the book introduces you to the MVC paradigm and teaches you how to architect rich Internet applications operating with basic concepts of Backbone.js. During the reading of this book you will learn how to solve challenging problems leveraging Backbone objects such as models, collections, views, routers, and so on.</p> <p><br />You learn how to use forms, layouts, templating engines, and other Backbone extensions, which will help you to complete specific features of your application. You will understand how to bind a model to a DOM element. You will see how perfectly Backbone.js integrates with third party libraries and frameworks such as jQuery, Zepto, Underscore.js, Require.js, Mustache.js, Twitter Bootstrap, jQueryMobile, PhoneGap and many others. This book will guide you in how to optimize and test your applications, create your own Backbone extensions, and share them with the open source community.</p> <p><br />With the help of Backbone.js Cookbook, you will learn everything you need to know to create outstanding rich Internet applications using the JavaScript programming language.</p>
Table of Contents (16 chapters)
Backbone.js Cookbook
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Welcome to the Backbone.js Cookbook. We will learn how to create outstanding web applications using lightweight JavaScript framework known as Backbone.js and utilizing the superior rendering power of modern browsers.

Backbone.js Cookbook contains a series of recipes that provide practical, step-by-step solutions to the problems that may occur during the frontend application development, using an MVC pattern and a REST-style communication. You will learn how to build Backbone applications by utilizing the power of popular Backbone extensions and how to integrate your app with different third-party libraries. You will also learn how to fulfill the requirements of the most challenging tasks.

What this book covers

Chapter 1, Understanding Backbone, introduces you to an MVC pattern and Backbone.js framework. You will learn how to design Backbone applications in terms of MVC and will be able to create your first Backbone app using models, views, and routers.

Chapter 2, Models, helps you learn about Backbone.Model, the main building block of your application, which stores data and provides business logic.

Chapter 3, Collections, teaches you how to organize models in manageable sets known as collections, which allow you to perform different methods, such as sorting, filtering, iterating, and so on.

Chapter 4, Views, helps you learn how to use Backbone views to render models and collections, and how to intercept DOM events.

Chapter 5, Events and Bindings, tells you about event system used in Backbone.js and demonstrates event binding techniques.

Chapter 6, Templates and UX sugar, is devoted to the frontend enhancements that makes Backbone application look better and program easily.

Chapter 7, REST and Storage, is focused on how Backbone.js synchronizes models and collections with a RESTful backend or stores them in the HTML5 local storage.

Chapter 8, Special Techniques, helps you learn how to solve the most challenging problems that can occur during Backbone development, such as creating extensions, testing your app, creating a mobile app, and performing search-engine compatibility.

What you need for this book

Most of the recipes in this book do not require special software to be used. What you need is a browser and a text editor or IDE to edit HTML, JavaScript, and CSS files. Some of the recipes in Chapter 7, Rest and Storage and Chapter 8, Special Techniques require you to install GIT, Node.js, and NPM. It also assumes that you can use a Unix-like shell.

Who this book is for

This book is created for frontend developers who are familiar with JavaScript, HTML, and CSS. It assumes that you have good understanding of Object Oriented Programming (OOP) and some practice with the jQuery library.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "To check if model has an attribute use the has() method. It returns true if the attribute exists, otherwise false."

A block of code is set as follows:

if (!invoiceItemModel.has('quantity'))
  {
    console.log('Quantity attribute does not exists!')
  }

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

var InvoiceItemModel = Backbone.Model.extend
  ({
    // Define validation criteria.
    validate: function(attrs) {
      if (attrs.quantity <= 0) {
         return "quantity can't be negative or equal to zero";
      }
    }
  });

Any command-line input or output is written as follows:

$ npm install -g requirejs

New terms and important words are shown in bold. Words that you see on the screen, in menus, or dialog boxes for example, appear in the text like this: "When user clicks on the Add button, the following popup is generated and shown to the user:".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.