What are Models?

As we learned in the Quick Tour chapter, Backbone Models manage our data. A Model's raison d'etre is to manage data outside of the DOM. Why?

Separating the concerns of an application makes it easier to understand, maintain, change, and test our code. In Backbone, a fundamental separation of concerns exists between Models and Views. The core concern of Models is not merely the storage of data, as stated above, but rather the expression of our application's domain logic, also known as business logic. Domain logic is very distinct from the infrastructure logic that makes software applications work. (Backbone Models blur the line a little by providing infrastructure for events and for interacting with a persistent data store.) Most importantly, domain logic should be isolated from presentation logic, which lives in Views, templates, and Routers, interacts with the DOM, and is often a large and intricate part of our application. "Get your truth out of the DOM" is a popular rallying cry for JavaScript frameworks like Backbone. The point of such frameworks is to help us enforce this division.

So, how do Models contribute to their side of the task? Models are the true workhorse of Backbone. Although many of our interactions focus on Collections, these in turn rely heavily on Models behind the scenes.


The rest of this chapter is part of the interactive book Backbone + CoffeeScript.