Beginning Backbone.js James Sugrue Beginning Backbone.js Copyright © 2013 by James Sugrue This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation are brief excerpts in connection with reviews or scholarly analysis or material supplied specifically for the purpose of being entered and executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this publication or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version, and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law. ISBN-13 (pbk): 978-1-43026334-0 ISBN-13 (electronic): 978-1-4302-6335-7 Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights. While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein. President and Publisher: Paul Manning Lead Editor: Louise Corrigan Technical Reviewer: Chris Wiegman Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Louise Corrigan, James DeWolf, Jonathan Gennick, Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Steve Weiss, Tom Welsh Coordinating Editor: Kevin Shea Copy Editor: Kim Wimpsett Compositor: SPi Global Indexer: SPi Global Artist: SPi Global Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail orders- [email protected], or visit www.springeronline.com. For information on translations, please e-mail [email protected], or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales. Any source code or other supplementary materials referenced by the author in this text is available to readers at www.apress.com. For detailed information about how to locate your book’s source code, go to www.apress.com/source-code. Contents at a Glance About the Author About the Technical Reviewer Acknowledgments Introduction Chapter 1: An Introduction to Backbone.js Chapter 2: Getting Object-Oriented Chapter 3: Backbone Models and Collections Chapter 4: Backbone View and Templating Libraries Chapter 5: Routers and Events Chapter 6: From Start to Finish: A Complete App Example Chapter 7: The Backbone Ecosystem Chapter 8: Testing Your Backbone.js Application Chapter 9: Using Grunt for Your Build Process Chapter 10: Extending Backbone with Marionette and Thorax Chapter 11: Best Practices with Backbone Chapter 12: Creating a Manageable JavaScript Code Base Index Contents About the Author About the Technical Reviewer Acknowledgments Introduction Chapter 1: An Introduction to Backbone.js What Is Backbone.js? Mission Statement Design Patterns for Web Applications Model View Controller Model View * How Backbone Supports Model View * Other JavaScript Model View * Frameworks Backbone Adoption in the Real World Companies Using Backbone Five Reasons to Use Backbone in Your Project Three Reasons Backbone Might Not Be Right for You Getting Backbone Set Up Downloading Backbone Testing Your Setup: Backbone Project Structure Some Tips for Your Development Environment Summary Chapter 2: Getting Object-Oriented The Rise of JavaScript JavaScript Versions Executing JavaScript Snippets What Is an Object Anyway? JavaScript: The Basics Primitive Data Types Core Objects in JavaScript Basic Syntax Declaring Variables Loops and Conditionals Arrays Closures Making JavaScript Object-Oriented Creating a Simple Object in JavaScript Defining Constructors Creating Methods JavaScript Prototype Encapsulation Inheritance Using Prototype Controlling Access to Methods and Properties Providing a Namespace Object-Oriented Frameworks for JavaScript PrototypeJS MooTools Backbone.js Summary Chapter 3: Backbone Models and Collections System Setup Backbone Models Constructors Model Attributes Adding Functions to Your Model Model Events Model Validation Exchanging Data with the Server Extending Your Own Models Backbone Collections Constructors Manipulating Collections Traversing Collections Other Utility Methods Exchanging Data with the Server Collection Quick Reference Summary Chapter 4: Backbone View and Templating Libraries Code Setup Backbone Views Creating a Backbone View Binding to the Physical View Rendering Content View Events Dealing with self and this Backbone View Quick Reference Templating in Backbone Templating with Underscore Templating with Handlebars Using Handlebars with Backbone A Quick Guide to Handlebars Providing Helpers Precompiling Templates Templating with Mustache Using Mustache with Backbone A Quick Guide to Mustache Which Templating Library to Use? Summary Chapter 5: Routers and Events Backbone Routers Creating a Router Backbone.History Using pushState Routes for All Applications Adding Parameters to Routes Route Events Manually Creating Routes Navigating Programmatically The Router as a Controller Backbone Events Binding Events Unbinding Events Listening to an Event Once Listen to Events on Other Objects Triggering Events Built-in Events Creating Custom Events Listening for DOM Events Summary Chapter 6: From Start to Finish: A Complete App Example Designing Your Application CSS Styles Application Domain Application Code Structure External Dependencies Bringing It All Together First Steps: Dealing with Authentication Getting Set Up on Twitter Keeping Secrets on Servers Showing the Twitter Timeline Adding the Profile View Profile Data Layer Profile View Putting It All Together Reusing the Profile View Making the Profile Model Reusable Changing the Behavior on the Timeline Adding Events to the Timeline View Creating a Pop-up Dialog for Profiles Adding Search Functionality The Data Layer Adding the Search View Sharing Models Between Backbone Objects Error Handling Summary Chapter 7: The Backbone Ecosystem Code Samples User Interface Components LayoutManager Backgrid Backbone UI Backbone.stickit Backbone.Notifications Models and Collection Extensions Backbone.trackit Backbone.memento Backbone.localStorage Backbone.dualStorage Backbone.ViewModel Backbone-Validator
Description: