The Client-Side State Transition
For years, web development relied on server-side rendering (PHP, ASP.NET, Java). Every user action (like sorting a table or submitting a form) required a full page refresh, making applications feel slow.
In early 2012, the industry is standardizing on Single Page Applications (SPAs). In this model, the browser loads a single HTML shell, and JavaScript handles rendering and state updates dynamically.
Backbone.js provides the architectural structure needed to manage client-side state.
Core Backbone.js Components
Backbone uses a lightweight MVC pattern:
1. Models and Collections
Models contain application data and validation rules. Collections are ordered sets of models that synchronize with RESTful backends automatically:
// Model definition in early 2012 Backbone.js
const Post = Backbone.Model.extend({
urlRoot: '/api/posts'
});
const myPost = new Post({ id: 5 });
myPost.fetch(); // Fires GET /api/posts/5 automatically2. Views and Templates
Views manage DOM events and render content using templates (like Underscore.js templates), decoupling layout configurations from data structures.
3. Routers
Using hash fragments (e.g. #users/801) or HTML5 History API to navigate between application views without reloading the page, establishing modern SPA client patterns.