We JavaScript developers live in exciting times. Every month we're presented with an incredible new browser feature, open source library, or software methodology that promises to vastly improve on whatever came before it. And then, just as soon as one new technology arrives, another almost inevitably follows it, promising still further innovation.
Amid this ever-changing landscape of new technologies only a very few libraries manage to stay not just relevant but essential over a long period of time, despite having newer challengers. For many web programmers jQuery was the first such library, but in recent years another library has proven itself to be similarly indispensable. That library is Backbone.
Backbone offers developers a wide range of foundational pieces from which they can build any manner of web application. From its simple yet flexible class system, to its event-based and AJAX-simplifying data containers, to its DOM manipulation and single-page user-facing components, Backbone provides everything needed to form the underlying framework of a site.
However, at first Backbone's incredible power and flexibility can be intimidating. When faced with over a hundred different methods spread across four base classes it can be challenging for a new Backbone developer to determine which ones to use and when. In addition, while Backbone is very "opinionated" about its core functions, it takes a deliberately agnostic approach to just about everything else. This allows developers to choose the perfect approach for their application, but at the same time all those choices can be daunting to someone unfamiliar with Backbone.
In this book we offer two things, both drawn from years of experience using Backbone to create and maintain a real-world web-based application. First, we will provide you with an understanding of all of Backbone's essentials. By the time you complete this book, you will have learned everything you need to create powerful and maintainable web applications using Backbone.
But at the same time, in addition to just explaining Backbone itself, we'll also explain the wider, "meta" level of Backbone programming. From advice on how to implement important details of your classes, to examinations of how Backbone can be made even more powerful when connected to the larger JavaScript ecosystem, we have endeavored to not only show how to use Backbone, but how to use it well.
Welcome to Backbone Essentials.
Chapter 1, Building a Single-Page Site Using Backbone, introduces Backbone and explains why it is such a popular choice of framework
Chapter 2, Object-Oriented JavaScript with Backbone Classes, details Backbone's class system, a significant improvement over JavaScript's native system
Chapter 3, Accessing Server Data with Models, begins our exploration of Backbone's data management, event listening, and AJAX capabilities with Backbone's Model class
Chapter 4, Organizing Models with Collections, continues our exploration of Backbone's data management capabilities, only this time using multiple data sets with the Collection class
Chapter 5, Adding and Modifying Elements with Views, examines Backbone's DOM rendering and event-handling View class
Chapter 6, Creating Client-side Pages with Routers, introduces the heart of Backbone's single-page architecture, the Router class
Chapter 7, Fitting Square Pegs in Round Holes – Advanced Backbone Techniques, explores advanced Backbone patterns for solving tricky problems
Chapter 8, Scaling Up – Ensuring Performance in Complex Applications, looks at the causes of performance issues in Backbone applications, and how to prevent them
Chapter 9, What Was I Thinking? Documenting Backbone Code, considers different strategies for documenting your application, including JSDoc and Docco
Chapter 10, Keeping the Bugs Out – How to Test a Backbone Application, recommends best practices for testing a Backbone application, with examples from the Mocha and Sinon libraries
Chapter 11, (Not) Re-Inventing the Wheel – Utilizing Third-Party Libraries, previews a variety of 3rd party libraries, some Backbone-specific and some not, which can benefit a Backbone application
Chapter 12, Summary and Further Reading, looks back on the topics previously discussed and considers their application in the real world
As a web-based technology book, Backbone Essentials requires very little, only some form of text editor and web browser. Backbone itself is compatible with almost all browsers, including Internet Explorer 7 and up, though a modern web browser is recommended. Similarly, while you can write Backbone code using a plain text editor, we recommend instead using a modern IDE such as Sublime, Eclipse, or WebStorm, as it can provide many useful features to aid development.
Of course to use Backbone you'll need to download (and include in your HTML) the library itself, which can be found at http://www.backbonejs.com. In addition, you'll also need to download Backbone's two dependencies: jQuery (http://www.jquery.com) and Underscore (http://www.underscorejs.org/). Technically only Underscore is required, but you'll want jQuery also to be able to take advantage of Backbone's View class.
Once you have an editor, browser, and all three libraries downloaded and included in your HTML, you're ready to get started using Backbone.
This book is geared towards readers with a basic understanding of JavaScript and HTML and at least some familiarity with the jQuery library. While this book does not require any formal computer science knowledge, it does employ industry terms such as "reference" or "inheritance system" which may be unfamiliar to readers without at least some programming background.
If you are unfamiliar with jQuery, we recommend that you first read Packt's excellent Learning jQuery, by Jonathan Chaffer, before starting this book. If you are completely new to web development, you would be better served by first familiarizing yourself with JavaScript through one of the many excellent free tutorials available on the web.
In this book, you will find a number of text styles 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: "We can include other contexts through the use of the include
directive."
A block of code is set as follows:
<tr> <td>Fake Book</td> <td>This is a description of a fake book</td> <td><a href=""/buy/book1"">Buy Fake Book</a></td> </tr>
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "For instance, you might have a Submit button on a page and after it triggers a save of the page's Model you want it to redirect the user to a different route."
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked about it. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>
, and mention the book's title in 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 at www.packtpub.com/authors.
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.
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. 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.
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 could 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 to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted 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 <[email protected]>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at <[email protected]>
, and we will do our best to address the problem.