Skip to content

Instantly share code, notes, and snippets.

@sbaldwin24
Created October 5, 2014 19:14
Show Gist options
  • Save sbaldwin24/40790d1c4908740c6b18 to your computer and use it in GitHub Desktop.
Save sbaldwin24/40790d1c4908740c6b18 to your computer and use it in GitHub Desktop.
Models in Backbone

Models in Backbone

A model contains the state (i.e. the JSON data or what Backbone calls attributes) for an application, as well as a basic set of functionality (i.e. methods like .set() or .get() and events like change) for managing and listening to changes in the state/data.

Additionally, the model orchestrates the storing of this state/data in memory in the browser, as well as persisting the state/data to the database on the server (i.e. syncing).

Collections

An ordered grouping of models is a collection.

In Backbone, collections are wrapped with a basic set of functionality for working with groups of models. The classic illustration of a model would be the data for a to-do item (i.e. the to-do and its state of completion), and a list of to-dos would be considered a collection of to-do models.

If you think in terms of a table, the table would be the collection (i.e. to-dos), the headers in the table would be the model (i.e. title, status), and the content in the table rows would be the data (i.e. get a dog, done).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment