Backbone.js Patterns and Best Practices
eBook - ePub

Backbone.js Patterns and Best Practices

Swarnendu De

  1. 174 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Backbone.js Patterns and Best Practices

Swarnendu De

Book details
Book preview
Table of contents
Citations

About This Book

In Detail

Backbone.js is a super light framework that allows you to structure your JavaScript codes in an MV* fashion. This framework is an excellent tool when it comes to creating an organized and modular code base for web apps of any size or complexity. Although lightweight, Backbone.js results in lots of boilerplate. Learning the best practices and design patterns will help you avoid these problems and allow you to ensure that the best standards are followed.

Backbone.js Patterns and Best Practices is packed with examples that will help you work with Backbone.js components. It also gives solutions to common problems faced by developers. It gives a complete overview of plugin development, large scale application architecture, and unit testing as well.

Starting with a discussion of why reducing boilerplate in your JavaScript code is essential, Backbone.js Patterns and Best Practices explains how to enforce reusability in your code by creating plugins and mixins. You will learn about patterns and best practices for Backbone views, models, collections, routers, and events.

You will discover solutions to common problems that developers face through simple examples, and delve into the best open source plugins available to solve these problems when they arise. This book is an excellent collection of design and implementation patterns that will help you structure and develop complex Backbone.js applications easily.

Approach

This is a step-by-step guide to design patterns, best practices, and solutions to common problems for Backbone.js-based application development.

Who this book is for

This book is for JavaScript developers who work with Backbone.js and want to learn the best design patterns to develop complex web applications. Basic knowledge of Backbone.js and JavaScript is essential.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Backbone.js Patterns and Best Practices an online PDF/ePUB?
Yes, you can access Backbone.js Patterns and Best Practices by Swarnendu De in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in JavaScript. We have over one million books available in our catalogue for you to explore.

Information

Year
2014
ISBN
9781783283576
Edition
1

Backbone.js Patterns and Best Practices


Table of Contents

Backbone.js Patterns and Best Practices
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Reducing Boilerplate with Plugin Development
Basic components of Backbone.js
Using Underscore.js
Re-using code with extensions
Creating a base class
Developing plugins without extending base classes
Understanding JavaScript mixins
What is a mixin?
Creating classic mixins
Creating functional mixins
Caching mixin functions
Using curry to combine a function and arguments
Summary
2. Working with Views
Basic usage of views
Understanding the el property
Listening to view events
Displaying model data with templates
Partially updating a view
Understanding nested views
Knowing when to use subviews
Avoiding multiple DOM reflow
Re-rendering parent views
Removing parent views
Working with templates
Storing templates in an HTML file
Storing templates in a JavaScript file
Precompiling templates
Avoiding evaluation in templates
Using template helper functions
Understanding automatic model-view data binding
Using Marionette's ItemView, CollectionView, and CompositeView
ItemView
CollectionView
Working with CompositeView
Using Layout Manager
Summary
3. Working with Models
Basic usage of models
Using the defaults property
Avoiding object references in the defaults property
Data interaction with the server
Creating a model
Updating a model
Fetching a model
Deleting a model
Validating the data
Using the Backbone.Validation plugin
Configuring validation rules
Prevalidating a model with the preValidate() method
Serializing models
Understanding the relational data model
Summary
4. Working with Collections
Basic usage of collections
Performing data operations with collections
Fetching data from the server
Saving data to the server
Sorting a collection
Sorting a collection with multiple attributes
Filtering a collection
Performing basic filtering
Filtering a collection with a duplicate collection
Self-filtering with full data pointers
Understanding the collection of multiple model types
Summary
5. Routing Best Practices and Subrouting
Working with routers
Best practices for working with routers
Avoiding large functional code in route methods
Instantiating views in router methods
Using regular expressions for selective routing
Subrouting – a key to organizing complex apps
The all-in-one router
The base router
The Users module router
Summary
6. Working with Events, Sync, and Storage
Understanding custom events
A simple case study on custom events
Using an event dispatcher
Creating multiple event dispatchers
Using different event namespaces
Avoiding memory leaks with the listenTo() method
Overriding the Backbone.sync() method
Offline storage using the Backbone.LocalStorage adapter
Summary
7. Organizing Backbone Applications – Structure, Optimize, and Deploy
Understanding the application directory structure
Working with Asynchronous Module Definition
Adding Require.js to your project
Configuring dependencies
Defining a module
Creating application architecture
Managing a project directory
Organizing code with AMD
Setting up an application
Using the module pattern
Managing objects and module communication
Using the observer/PubSub pattern
Using the mediator pattern
Understanding view management
Understanding other important features
Summary
8. Unit Test, Stub, Spy, and Mock Your App
Understanding why unit testing is important
Testing with QUnit
Performing a basic test case
Understanding the QUnit module (), setup (), and teardown () methods
Testing Backbone.js components with QUnit
Using test spies, stubs, and mocks with SinonJS
Testing with spies
Testing with stubs
Testing with mocks
Difference between mocks and stubs
Summary
A. Books, Tutorials, and References
Books for reference
Tutorials
Unit testing
Other plugins and tutorials
B. Precompiling Templates on the Server Side
C. Organizing Templates with AMD and Require.js
Precompiling with the requirejs-tpl plugin
Index

Backbone.js Patterns and Best Practices

Copyright © 2014 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: January 2014
Production Reference: 1200114
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78328-357-6
www.packtpub.com
Cover Image by Manali Mandal ()

Credits

Author
Swarnendu De
Reviewers
Marc D. Bodley
Florian Bruniaux
Philippe Charrière
Ezekiel Chentnik
Lorenzo Pisani
Acquisition Editors
Mary Jasmine Nadar
Gregory Wild
Lead Technical Editor
Ruchita Bhansali
Technical Editors
Vrinda Amberkar Bhosale
Shubhangi H. Dhamgaye
Shweta Pant
Ritika Singh
Copy Editors
Janbal Dharmaraj
Sayanee Mukherjee
Project Coordinator
Sageer Parkar
Proofreader
Ameesha Green
Indexer
Hemangini Bari
Graphics
Abhinash Sahu
Production Coordinator
Arvindkumar Gupta
Cover Work
Arvindkumar Gupta

About the Author

Swarnendu De is the director of Innofied Solution Pvt. Ltd. (http://www.innofied.com), a specialized mobile, web, and game development company. He manages technical operations and leads the JavaScript development team there. For the last seven years, he has been working with numerous JavaScript technologies including Backbone.js, Node.js, ExtJS, Sencha, and so on, and has developed more than 50 complex JavaScript-based applications thus far. He regularly writes at his personal blog, company blog, and the Tuts+ network. He has been working with Backbone.js for the last 2 years and has developed multiple, large, and complex Backbone.js-based applications using this technology.
Swarnendu lives in Kolkata—the city of joy. He loves travelling, photography, and spending time with his family. You can reach him through his website at http://www.swarnendude.com or via Twitter at @swarnendude.

Acknowledgments

Writing such a book is quite hard, especially when you are busy managing the operations and technical team at your own startup. I would like to thank my business partner, Sandip Saha, who shared the workload so that I could spend more time completing this book. Special thanks to my senior, Saikat Sengupta, who did all the editing and proofreading for each chapter—the whole journey wouldn't have been this smooth without his help.
Writing this book would never have been possible without the help of the Backbone community who have contributed to all the technologies that I have used in this...

Table of contents

  1. Backbone.js Patterns and Best Practices