Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Backbonification - Migrating NewsBlur From DOM ...

samuelclay
November 15, 2012

Backbonification - Migrating NewsBlur From DOM Spaghetti to Backbone.js

This talk explores patterns, techniques, and common pitfalls in migrating from vanilla JavaScript to Backbone.js. It covers moving routers, models, and views, and the process used to migrate a living app.

samuelclay

November 15, 2012
Tweet

Other Decks in Programming

Transcript

  1. Backbonification Migrating NewsBlur from DOM spaghetti to Backbone.js Samuel Clay

    November 2012 Backbone.js patterns and pitfalls — covering —
  2. Background • Started NewsBlur as a side-project in 2009 •

    Open-source since the beginning • DocumentCloud alum
  3. Folder Feed Unread count Feed list header Feed list Unread

    count Reading pane Feed (story view) Story title Original/Feed view Intelligence Level
  4. Task at hand • Small handful of JavaScript files •

    8,500 lines in biggest file • One file for templates, drawing, delegation, element caches, state, view modes
  5. Performance • Only necessary calculations were made • Tree traversal

    time was eliminated 0 100 200 300 JavaScript Backbone.js feed page load (milliseconds) 0 325 650 975 1,300 JavaScript Backbone.js folder draw (milliseconds)
  6. Moving routers • It’s where you can stick those out-of-

    band ajax calls • Anything having to do with the url
  7. Moving models • The schema coming from the server •

    May cause versioning • Server should be sending lists of dictionaries
  8. Moving models • Transparently append the API version as a

    parameter by overriding your collection’s fetch method.
  9. Moving models • Pass a Backbone model's model.attributes to old

    JavaScript methods • When done migrating, clean up by looking for .attributes
  10. Moving models • Populating a collection that has side-effects •

    Rendering child views before the parent view means they will be attached to the wrong parent view • Pass {silent: true} to the initial reset, then manually trigger the reset event with Collection.change() • Another option is to use _.defer()
  11. Moving models • Listening for events on a collection's models

    • For convenience, model events also show as collection events
  12. Moving models • Bind to the change event but only

    update on specific attributes using model.hasChanged() and model.previousAttributes()
  13. Moving models • Listening to the change event instead of

    change:attribute because change is fired last
  14. Moving views • Changing the top-level element of a view

    • But don’t forget about the element on page if you’re re-rendering the view
  15. Moving views • Traversing a view by keeping track of

    the active view in the collection
  16. Common pitfalls • TypeError: 'undefined' is not an object (evaluating

    'func.bind') • Comes from trying to bind to a method that doesn't exist
  17. Common pitfalls • Firing a change event while still setting

    up models and views • Add {silent: true} to a model.set() call if you're not ready to handle the change events • When views are all setup, blast out a .change()
  18. Common pitfalls • Re-rendering the view just to get a

    class toggled on or off • Selectively re-render/toggle classes based on specific change events
  19. Common pitfalls • Cleanup of ghost views • The model

    still has bindings to the destroyed view
  20. Resources that I liked • Developing Backbone.js Applications by Addy

    Osmani addyosmani.github.com/backbone-fundamentals • Backbone Patterns by Rico Sta. Cruz ricostacruz.com/backbone-patterns