Developing Backbone.js Applications
Read it now on the O’Reilly learning platform with a 10-day free trial.
O’Reilly members get unlimited access to books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.
Book description
If you want to build your site’s frontend with the single-page application (SPA) model, this hands-on book shows you how to get the job done with Backbone.js. You’ll learn how to create structured JavaScript applications, using Backbone’s own flavor of model-view-controller (MVC) architecture.
Start with the basics of MVC, SPA, and Backbone, then get your hands dirty building sample applications—a simple Todo list app, a RESTful book library app, and a modular app with Backbone and RequireJS. Author Addy Osmani, an engineer for Google’s Chrome team, also demonstrates advanced uses of the framework.
- Learn how Backbone.js brings MVC benefits to the client-side
- Write code that can be easily read, structured, and extended
- Work with the Backbone.Marionette and Thorax extension frameworks
- Solve common problems you’ll encounter when using Backbone.js
- Organize your code into modules with AMD and RequireJS
- Paginate data for your Collections with the Backbone.Paginator plugin
- Bootstrap a new Backbone.js application with boilerplate code
- Use Backbone with jQuery Mobile and resolve routing problems between the two
- Unit-test your Backbone apps with Jasmine, QUnit, and SinonJS
Show and hide more
Publisher resources
Table of contents Product information
Table of contents
- Developing Backbone.js Applications
- Preface
- Target Audience
- Credits
- Reading
- Conventions Used in This Book
- Using Code Examples
- Safari® Books Online
- How to Contact Us
- Acknowledgments
- What Is MVC?
- What Is Backbone.js?
- When Do I Need a JavaScript MVC Framework?
- Why Consider Backbone.js?
- Setting Expectations
- MVC
- Smalltalk-80 MVC
- MVC Applied to the Web
- Client-Side MVC and Single-Page Apps
- Client-Side MVC: Backbone Style
- Implementation Specifics
- Models
- Views
- Templating
- Handlebars.js
- Underscore.js microtemplates
- Controllers
- Delving Deeper into MVC
- Summary
- Further Reading
- Backbone.js
- Used by
- Getting Set Up
- Models
- Initialization
- Default values
- Model.get()
- Model.set()
- Direct access
- Creating New Views
- What Is el?
- $el and $()
- setElement
- Understanding render()
- The events hash
- Adding and Removing Models
- Retrieving Models
- Listening for Events
- Resetting/Refreshing Collections
- Underscore Utility Functions
- forEach: Iterate over collections
- sortBy(): Sort a collection on a specific attribute
- map(): Create a new collection by mapping each value in a list through a transformation function
- min()/max(): Retrieve item with the min or max value of an attribute
- pluck(): Extract a specific attribute
- filter(): Filter a collection
- indexOf(): Return the item at a particular index within a collection
- any() : Confirm if any of the values in a collection pass an iterator truth test
- size(): Return the size of a collection
- isEmpty(): Determine whether a collection is empty
- groupBy(): Group a collection into groups like items
- pick(): Extract a set of attributes from a model
- omit(): Extract all attributes from a model except those listed
- keys() and values(): Get lists of attribute names and values
- pairs(): Get list of attributes as [key, value] pairs
- invert(): Create object in which the values are keys and the attributes are values
- Fetching Models from the Server
- Saving Models to the Server
- Deleting Models from the Server
- Options
- on(), off(), and trigger()
- listenTo() and stopListening()
- Events and Views
- Backbone.history
- Overriding Backbone.sync
- Static HTML
- Header and Scripts
- Application HTML
- Templates
- Setting Up
- Creating the Model, Collection, Views, and App
- Adding Models
- Removing Models
- Install Node.js, npm, and MongoDB
- Install Node Modules
- Create a Simple Web Server
- Connect to the Database
- MarionetteJS (Backbone.Marionette)
- Boilerplate Rendering Code
- Reducing Boilerplate with Marionette.ItemView
- Memory Management
- Region Management
- Marionette Todo App
- TodoMVC.js
- TodoMVC.Layout.js
- TodoMVC.TodoList.js
- Controllers
- CompositeView
- TodoMVC.TodoList.Views.js
- Todos.js
- Hello World
- Embedding Child Views
- View Helpers
- collection Helper
- Custom HTML Data Attributes
- Thorax Resources
- Working with Nested Views
- Problem
- Solution 1
- Solution 2
- Solution 3
- Solution 4
- Problem
- Solution
- Problem
- Solution
- Problem
- Solution
- Problem
- Solution
- Problem
- Solution
- Problem
- Solution
- Backbone.validateAll
- Backbone.Validation
- Form-Specific Validation Classes
- Problem
- Solution
- Problem
- Solution
- Calling Overridden Methods
- Backbone-Super
- Problem
- Solution
- Event Aggregator
- Backbone’s event aggregator
- jQuery’s event aggregator
- A mediator for Backbone
- Events
- Third-party objects
- Event aggregator use
- Mediator use
- Organizing Modules with RequireJS and AMD
- Maintainability Problems with Multiple Script Files
- Need for Better Dependency Management
- Asynchronous Module Definition (AMD)
- Writing AMD Modules with RequireJS
- Alternate syntax
- RequireJS configuration
- RequireJS Shims
- Wrapping models, views, and other components with AMD
- Overview
- Markup
- Configuration Options
- Modularizing Our Models, Views, and Collections
- Route-Based Module Loading
- JSON-Based Module Configuration
- Module Loader Router
- Using NodeJS to Handle pushState
- Backbone.Paginator
- Live Examples
- Convenience Methods
- Convenience Methods
- Implementation Notes
- Plug-ins
- Bootstrapping
- Styling
- Summary
- Getting Started
- Creating a New Project
- index.html
- config.js
- main.js
- app.js
- Creating Backbone Boilerplate Modules
- router.js
- Yeoman
- Backbone DevTools
- Mobile App Development with jQuery Mobile
- The Principle of Progressive Widget Enhancement by jQMobile
- Understanding jQuery Mobile Navigation
- Routing to a Concrete View Page, Inheriting from BasicView
- Management of Mobile Page Templates
- DOM Management and $.mobile.changePage
- Dynamic DOM Scripting
- Intercepting jQuery Mobile Events
- Performance
- Clever Multiplatform Support Management
- Behavior-Driven Development
- Suites, Specs, and Spies
- beforeEach() and afterEach()
- Shared Scope
- Getting Set Up
- TDD with Backbone
- Models
- Collections
- Views
- View Testing
- Initial setup
- View rendering
- Rendering with a templating system
- Getting Set Up
- Sample HTML with QUnit-Compatible Markup
- Basic Test Case Using test( name, callback )
- Comparing the Actual Output of a Function Against the Expected Output
- Basic QUnit Modules
- Using setup() and teardown()
- Using setup() and teardown() for Instantiation and Clean Up
- Fixtures Example
- What Is SinonJS?
- Basic Spies
- Spying on Existing Functions
- Inspection Interface
- Matching arguments: Test that a spy was called with a specific set of arguments
- Stricter argument matching: Test that a spy was called at least once with specific arguments and no others
- Testing call order: Test that a spy was called before or after another spy
- Match execution counts: Test that a spy was called a specific number of times
- Stubs
- Mocks
- Models
- Collections
- Views
- App
- A Simple JavaScript MVC Implementation
- Event System
- Models
- Views
- Controllers
- Practical Usage
- Models, Views, and Presenters
- What Is Namespacing?
- Single global variables
- Object literals
- Nested namespacing
- DOM Manipulation
- Utilities
- RESTful Persistence
- Routing
- Books and Courses
- Extensions/Libraries
Show and hide more
Product information
- Title: Developing Backbone.js Applications
- Author(s): Addy Osmani
- Release date: May 2013
- Publisher(s): O'Reilly Media, Inc.
- ISBN: 9781449328559