Slide 1

Slide 1 text

Backbone.js

Slide 2

Slide 2 text

Backbone = a simple library of objects that can be organized into complex but meaningful relationships

Slide 3

Slide 3 text

...unless they aren’t

Slide 4

Slide 4 text

Stateless Server-sent HTML Apps (not Backbone) Stateful Single-Page JS App (Backbone)

Slide 5

Slide 5 text

Object Basics ● Events (prototype object) ● Model ● Collection ● View ● Special Stuff: Router, History, App ● POJO

Slide 6

Slide 6 text

● Events ● Model ● Collection ● View ● Special Stuff ● POJO ● Prototype of all Backbone objects ● Can emit events ● Can listen to other objects for events to trigger callbacks Remember: state is maintained, all object instances are just hanging out in memory in your browser window

Slide 7

Slide 7 text

● Events ● Model ● Collection ● View ● Special Stuff ● POJO ● Stores attributes ● Syncs with persisted objects on the server ● Emit events when attributes are changed

Slide 8

Slide 8 text

● Events ● Model ● Collection ● View ● Special Stuff ● POJO ● A ‘collection’ of models ● Sync with persisted server objects ● Emit events when models change ● Functional sugar (filter, sort, etc)

Slide 9

Slide 9 text

● Events ● Model ● Collection ● View ● Special Stuff ● POJO ● Manages specific parts of DOM ● Has attached models or collections ● render() HTML to modify the DOM (with the help of a Template) ● Listens for changes on attached models/collections ● Can have childviews (it’s childviews all the way down)

Slide 10

Slide 10 text

● Events ● Model ● Collection ● View ● Special Stuff ● POJO ● Router: defines callbacks based on URL (instantiate models/collections and inject into a new high-level view) ● History: manages global state ● Application: Tie it all together at the highest level to be exposed to the browser via

Slide 11

Slide 11 text

● Events ● Model ● Collection ● View ● Special Stuff ● POJO Plain Old Javascript Objects ● Helper methods and calculated attributes ● Prototypical inheritance ● Decorator Pattern ● Composite/Strategy Pattern ● Singleton Pattern require(‘path/to/file) fetch dependencies and instantiated objects out of memory

Slide 12

Slide 12 text

Bootstrapping the app app = require 'application' User = require 'models/user' UserView = require 'views/page_view' module.exports = class Router extends Backbone.Router routes: 'users/:userId': 'showUser' showUser: (userId) -> user = new User id: userId userView = new UserView el: '#page' model: user app.views.push userView user.fetch()

Slide 13

Slide 13 text

module.exports = class UserView extends Backbone.View events: 'click .show-profile': 'showProfile' initialize:-> @listenTo @model, 'sync', @render @listenTo @model, 'change', @render render: -> # USE BACKBONE.LAYOUTMANAGER @$el.html UserTemplate firstName: @model.get('firstname') devSitesRemaining: @devSitesRemaining() devSitesRemaining: -> @model.get('maxdevsites') - @model.get('dev_sites_count') showProfile: -> profileView = new ProfileView model: @model.profile @childViews.push profileView @model.profile.fetch() UserTemplate = require 'views/user' ProfileView = require 'views/profileView'

Slide 14

Slide 14 text

Good 1. Instantiate 2. Inject 3. Trigger Side Effects user = new User id: userId userView = new UserView el: '#page' model: user user.fetch() Bad 1. Initialize 2. Instantiate 3. Trigger Side Effects class UserView extends Backbone.View initialize: (userId) -> @model = new User id: userId @model.fetch()

Slide 15

Slide 15 text

Backbone = not rocket science, not even that strange Template/DOM View