Backbone.js Cheatsheet

Lessons

  • Lesson 01 [2025-03-22]
  • Lesson 02 [2025-03-22]
  • Lesson 03 [2025-03-22]
  • Lesson 04 [2025-03-22]
  • Lesson 05 [2025-03-22]
  • Lesson 06 [2025-03-22]
  • Lesson 07 [2025-03-22]
  • Lesson 08 [2025-03-24]
  • Lesson 09 [2025-03-24]
  • Lesson 10 [2025-03-24]
  • Lesson 11 [2025-03-24]
  • Lesson 12 [2025-03-25]
  • Lesson 13 [2025-03-27]
  • Lesson 14 [2025-03-28]
  • Lesson 15 [2025-03-29]
  • Lesson 16 [2025-03-30]
  • Lesson 17 [2025-03-31]
  • Lesson 18 [2025-04-01]
  • Lesson 19 [2025-04-02]
  • Lesson 20
  • Lesson 21
  • Lesson 22
  • Lesson 23
  • Lesson 24
  • Lesson 25
  • Lesson 26
  • Lesson 27
  • Lesson 28
  • Lesson 29
  • Lesson 30
  • Lesson 31
  • Lesson 32
  • Lesson 33
  • Lesson 34
  • Lesson 35
  • Lesson 36
  • Lesson 37
  • Lesson 38
  • Lesson 39
  • Lesson 40
  • Lesson 41
  • Lesson 42
  • Lesson 43
  • Lesson 44
  • Lesson 45

Notes

  • Backbone is a lightweight library for structuring JS code. It operates under the MVC design framework.
  • MVC promotes separation of concerns to create easily extendible and loosely couple application while keeping code maintainable code
  • Backbone Components:
      1. Events: Ability for an object to pub/sub events
      1. Models: Application state
      1. Collections: A set of Models
      1. Views: Render models and listen for DOM/model events
      1. Routers: To create SPAs

Models

  • Models:
    • Containers for application data
    • JS objects with changing tracking mechanism and pub/sub and ajax request to server
      • person.save({ success: () => {}})
    • When a model is created new Model() Backbone automatically calls model.initialize() method
    • Backbone Models are hash maps.
      • Must use .set when setting Model attributes
      • Read with .get
      • Delete with .unset
      • Delete all with .clear
      • Get a JSON with .toJSON
      • Check for attribute existence with .has
      • When instantiating the model can use .defaults object to set default value for attributes
      • .validate() method called whenever attributes are set on a model ... must return undefined or string if not valid; method is passed model.attrs as a parameter
var Song = Backbone.Model.extend({
  validate: function (attrs) {
    if (!attrs.title) {
      return "Title is required";
    }
  },
});
  • Inheritance from a Parent model can be done via the var Child = Parent.extend() method on the Parent class