Skip to content

Instantly share code, notes, and snippets.

@timlinquist
Created December 12, 2011 18:44
Show Gist options
  • Save timlinquist/1468529 to your computer and use it in GitHub Desktop.
Save timlinquist/1468529 to your computer and use it in GitHub Desktop.
Backbone best practices

General

  • User activity should manipulate data, not modify markup : Let backbone handle updating the view when an action takes place with something like an observer.

Models (use a model when your problem is inline with the items below)

  • Works with attributes (properties of the object)
  • Automatically triggers data change events ie delete, update, etc
  • Can use custom methods
  • Can use javascript properties to reference other objects (even Collections; composition?)

Collections (use a collection when your problem is inline with the items below)

  • Manages an array of models
  • Can trigger custom events
  • Can become complicated when trying to recreate the functionality of a model

Views

  • Append List to the end of views that render a collection for clarity
  • Define initialize() and set any necessary properties in the actual view class (setup only happens once for life of application ie once view is first executed.) Caveat : You must do this after the page has loaded ie inside of a document.ready block
  • ALWAYS add the bindAll function in the initialize method of every view to ensure event binding happens in the right context otherwise callbacks can bind to the wrong events.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment