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

BackboneConf 2014

BackboneConf 2014

Keynote at BackboneConf talking about ampersand.js, amp, and intelligent DOM diffing with virtual DOM.

Henrik Joreteg

December 16, 2014
Tweet

More Decks by Henrik Joreteg

Other Decks in Programming

Transcript

  1. "Backbone is an attempt to discover the minimal set of

    […] primitives that are generally useful when building web applications with JavaScript."
  2. "In an ecosystem where overarching, decides- everything-for-you frameworks are commonplace

    […] — Backbone should continue to be a tool that gives you the freedom to design the full experience of your web application."
  3. "I’m more interested in optimizing a person’s understanding of problems

    than their understanding of solutions." - Kris Gale (former VPE Yammer)
  4. var model = new Backbone.Model({ name: 'henrik' }); model.on('change:name', function

    () { console.log('new changed'); }); model.set({name: 'bob'}); Backbone Models
  5. var Person = AmpersandState.extend({ props: { name:'string' } }); var

    model = new Model({name: 'henrik'}); model.on('change:name', someFunc); model.name = 'bob'; // still fires event model.name = 47; // throws TypeError
  6. var Person = AmpState.extend({ props: { name:'string' }, session: {

    active:'boolean' } }); var model = new Person({ name: 'henrik', active: true }); model.active; //=> true model.toJSON(); //=> {name: 'henrik'}
  7. var Person = AmpState.extend({ props: { today: 'date' } });

    // unix timestamp coming in var henrik = new Person({today: '1418338921707'}); // getter returns Date Object henrik.today; //=> JS `Date` instance // timestamp when serializing JSON.stringify(henrik); //=> {today: 1418338921707}
  8. var Person = AmpState.extend({ props: { name: 'string' }, derived:

    { nickName: { deps: ['name'], fn: function () { return this.name.slice(0, 3); } } } });
  9. var someone = new Person({name: 'henrik'}); someone.on('change:nickName', logChange); // computed

    only once and cached someone.nickName; //=> 'hen' // not triggered if result is same someone.name = 'henry'; // only if different someone.name = 'crazy'; // logs changed nick: 'cra'
  10. 1. derive from child models 2. derive from other derived

    3. useful for relationships between models 4. cannot set directly 5. resulting model code is more readable CACHED, EVENTED, DERIVED PROPERTIES
  11. "Another flaw in the human character is that everybody wants

    to build and nobody wants to do maintenance." - Kurt Vonnegut
  12. 1. SINGLE GITHUB REPO 2. CONTAINING 40+ MODULES 3. WRITE

    DOC 4. WRITE IMPLEMENTATION 5. WRITE TEST 6. RUN BUILD
  13. "In a finished Backbone app, you don't have to write

    the glue code that looks into the DOM to find an element with a specific id, and update the HTML manually — when the model changes, the views simply update themselves." - Backbone Docs
  14. THE SIMPLE WAY: // assume we have 'el' element //

    with this HTML: // "<div><input/></div>" // we can just replace its contents // with a new string: el.innerHTML = "<input/><p>hi!</p>"
  15. NOT IDEAL FOR LARGER SECTIONS OF DOM OR IF USER

    INPUTS NEEDS TO MAINTAIN FOCUS
  16. REACT + BACKBONE IN SUMMARY: var MyView = React.createClass({ getInitialState:

    function () { this.props.model = new Model(); this.props.model.on('change', this.forceUpdate, this); }, render: function () {
 return ( <div> <p>{this.model.name}</p> </div> ) } });
  17. INITIAL FINDINGS: 1. ~SAME PERFORMANCE AS REPLACE 2. FILESIZE: 7.25kb

    min + gzip 3. NESTED VIEW COMPONENTS WORK 4. CHOICE OF TEMPLATING LANGUAGE
  18. CURRENT FLOW: 1. any template language 2. HTML string 3.

    parse into AST 4. generate vdom 5. get diffs 6. apply DOM transforms
  19. CURRENT FLOW: 1. any template language 2. HTML string 3.

    parse into AST 4. generate vdom 5. get diffs 6. apply DOM transforms
  20. CURRENT FLOW: 1. any template language 2. HTML string 3.

    parse into AST 4. generate vdom 5. get diffs 6. apply DOM transforms