Lock in $30 Savings on PRO—Offer Ends Soon! ⏳

Getting Truth Out of the DOM

Yehuda Katz
September 27, 2011

Getting Truth Out of the DOM

Yehuda Katz

September 27, 2011
Tweet

More Decks by Yehuda Katz

Other Decks in Technology

Transcript

  1. Me

  2. native handlers browser event update data object data handler data

    object event DOM update Data-Centric Approach
  3. ✔Do Something native change handler set isDone = true on

    object object change handler recalculate remaining & update DOM
  4. ✔Do Something native change handler set isDone = true on

    object object change handler recalculate remaining & update DOM
  5. native change handler set isDone = true on object object

    change handler recalculate remaining & update DOM set isDone = true on object object change handler recalculate remaining & update DOM set isDone = true on object object change handler recalculate remaining & update DOM set isDone = true on object object change handler recalculate remaining & update DOM
  6. native change handler set isDone = true on object object

    change handler recalculate remaining & update DOM set isDone = true on object object change handler recalculate remaining & update DOM set isDone = true on object object change handler recalculate remaining & update DOM set isDone = true on object object change handler recalculate remaining & update DOM
  7. list.bind('change', function() { if(!list.bulk) { var remaining = list.remaining(); var

    text = remaining + ' remaining'; $("#stats").html(text); } });
  8. $(todo).bind('changeData', function(e, name) { if (name === 'isDone') { var

    todo = $(this); var check = todo.data('element'); check.attr(checked, todo.isDone); var ul = check.closest('ul'); var list = ul.data('list'); list.triggerLater('change'); } });
  9. native change handler set isDone = true on object object

    change handler triggerLater list change set isDone = true on object object change handler triggerLater list change set isDone = true on object object change handler triggerLater list change set isDone = true on object object change handler triggerLater list change
  10. native change handler set isDone = true on object object

    change handler triggerLater list change set isDone = true on object object change handler triggerLater list change set isDone = true on object object change handler triggerLater list change set isDone = true on object object change handler triggerLater list change
  11. ✔ Mark All Done Evolving User The Server What do

    you want to do? 1 item remaining
  12. ✔ Mark All Done Evolving User The Server What do

    you want to do? 1 item remaining View
  13. ✔ Mark All Done Evolving User The Server What do

    you want to do? 1 item remaining View
  14. ✔ Mark All Done Evolving User The Server What do

    you want to do? 1 item remaining Controller (App State)
  15. ✔ Mark All Done Evolving User The Server What do

    you want to do? 1 item remaining View
  16. ✔ Mark All Done Evolving User The Server What do

    you want to do? 1 item remaining Unchecked (Controller)
  17. ✔ Mark All Done Evolving User The Server What do

    you want to do? 1 item remaining View
  18. ✔ Mark All Done Evolving User The Server What do

    you want to do? 1 item remaining Checked (Model)
  19. ✔ Mark All Done Evolving User The Server What do

    you want to do? 1 item remaining Title (Model)
  20. ✔ ✔ Mark All Done Evolving User The Server The

    Client ✔ ✔ allAreDone (controller)
  21. ArrayController allAreDone Property allAreDone: function(key, value) { if (value !==

    undefined) { this.setEach('isDone', value); return value; } else { return this.get('length') && this.everyProperty('isDone', true); } }.property()
  22. getter allAreDone: function(key, value) { if (value !== undefined) {

    this.setEach('isDone', value); return value; } else { return this.get('length') && this.everyProperty('isDone', true); } }.property()
  23. setter allAreDone: function(key, value) { if (value !== undefined) {

    this.setEach('isDone', value); return value; } else { return this.get('length') && this.everyProperty('isDone', true); } }.property()
  24. Buffered Between Layers "Buffered Between Layers" Changes to the model

    layer for a particular event can all be made before they are propagated to the controller or view.
  25. While Handling Events ArrayController allAreDone dirty? This flag is automatically

    set by SproutCore if the property or any of its dependencies change.
  26. While Handling Events ArrayController allAreDone dirty? ✔ This flag is

    automatically set by SproutCore if the property or any of its dependencies change.
  27. Dependencies allAreDone: function(key, value) { if (value !== undefined) {

    this.setEach('isDone', value); return value; } else { return this.get('length') && this.everyProperty('isDone', true); } }.property('@each.isDone')
  28. So close! Because these template engines limit arbitrary logic, it

    should be possible to automatically UPDATE if the objects change
  29. <h1>Todos</h1> {{#view Todos.CreateTodoView}} <input id="new-todo" type="text" placeholder="Wot?" > {{/view}} {{#view

    Todos.StatsView}} <div class="remaining"> {{#view Todos.ClearCompletedView}} <button>Clear Completed Todos</button> {{/view}} {{displayRemaining}} remaining. </div> {{/view}}
  30. <label class='done-label'> {{view SC.Checkbox valueBinding= "Todos.todoListController.allAreDone"}} Mark All as Done

    </label> {{#each Todos.todoListController itemClassBinding="isDone"}} <label class='done'> {{view SC.Checkbox valueBinding= "parentView.content.isDone"}} <div class="todo-content"> {{content.title}} </div> </label> {{/each}}
  31. SC Views Manipulate DOM With jQuery SC.Checkbox = SC.View.extend({ value:

    function(key, value) { if(value !== undefined) { this.$('input').attr('checked', value); } else { value = this.$('input').attr('checked'); } return value; }.property() });
  32. Uniform Object Model The SproutCore Object Model wraps all of

    this up in a consistent, well-defined way to declare object relationships
  33. Data Store The SproutCore data store takes advantage of the

    declarative nature of SproutCore to let you lazily populate Arrays asynchronously. API TO BE IMPROVED