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

KnockoutJS and MVVM with JavaScript - TechED 2013

KnockoutJS and MVVM with JavaScript - TechED 2013

Do you write a lot of HTML and JavaScript code to push and pull data? In this session, learn popular techniques to use data binding to bind your data to your target controls in HTML writing less code, but gaining more power. See how to consume JSON data, use JSON objects in JavaScript, use declarative binding, using KnockoutJS. Also, see how to use the MVVM pattern to write data-centric Javascript code that follows good separation patterns and creates highly maintainable code.

John Papa

June 08, 2013
Tweet

More Decks by John Papa

Other Decks in Technology

Transcript

  1. <input data-bind="value: code" placeholder="Code"/> Declarative Binding var myViewModel = {

    code: ko.observable('DEV338') }; ko.applyBindings(myViewModel); Create an Observable Bind the ViewModel to the View
  2. <section data-bind=" "> <article> <div> <img data-bind=" " /> <address

    data-bind=" "></address> <address data-bind=" "></address> </div> </article> </section>
  3. <div> <label for="title">Title</label> <input id="title" data-bind="value: session().title"/> </div> <div> <label>Track</label>

    <select data-bind="options: tracks, optionsText: 'name', value: session().track"> </select> </div>
  4. var 'Haley' Read Values Using Parentheses 'Ella' 'Ella' Set Values

    Using Parentheses Function definition Good! Redefined name to be a string, not an observable Good!
  5. <section> <div data-bind="text: people().length"></div> <article data-bind="foreach: people"> <address data-bind="text:name"></address> <address

    data-bind="text:age"></address> <address data-bind="text:address().city"></address> </article> </section>
  6. vm = { id: ko.observable(1), salePrice: ko.observable(4199), qty: ko.observable(2) };

    vm.extendedPrice = ko.computed(function () { return this.product() ? this.salePrice() * parseInt("0" + this.qty(), 10) : 0; }, vm); observables
  7. var hasChanges = ko.computed(function() { return datacontext.hasChanges(); }); var canSave

    = ko.computed(function() { return hasChanges() && !isSaving(); });
  8. var vm = { salesPerson: ko.observable("John"), empNum: ko.observable(39811), products: ko.observableArray([

    { model: "Taylor 314CE", price: 1749, id=321 }, { model: "Martin D40", price: 1899, id=479 } ]) }; <span data-bind="text: products().length"></span> Pre-populating Operating on observableArray
  9. <input type="text" data-bind="enable: allowEdit, value: price" /> <select data-bind="options: colors,

    value: selectedColor, optionsText: 'name', optionsValue: 'key'" ></select> Built into Knockout Binding for Element Attributes Multiple Binding Expressions
  10. attr checked click css disable enable event hasfocus html options

    optionsText optionsValue selectedOptions style submit text uniqueName value visible
  11. attr checked click css disable enable event hasfocus html options

    optionsText optionsValue selectedOptions style submit text uniqueName value visible
  12. attr checked click css disable enable event hasfocus html options

    optionsText optionsValue selectedOptions style submit text uniqueName value visible
  13. attr checked click css disable enable event hasfocus html options

    optionsText optionsValue selectedOptions style submit text uniqueName value visible
  14. <section data-bind="foreach: sessions"> <article> <img data-bind="attr: { src: speaker().imageName }"

    class="img-polaroid"/> <span data-bind="text: timeSlot().name"></span> <span data-bind="text: room().name"></span> <small data-bind="text: level" class="right"></small> <small data-bind="text: track().name" class="right"></small> <small data-bind="text: code" class="right"></small> <h1 data-bind="text: title"></h1> <address data-bind="text: speaker().fullName"></address> <small data-bind="text: tagsFormatted"></small> </article> </section> For every session, render this article
  15. ko.bindingHandlers.fadeVisible = { init: function (element, valueAccessor) { }, update:

    function (element, valueAccessor, allBindingsAccessor) { } }; Runs first time the binding is evaluated Runs after init and every time one of its observables changes
  16. ko.bindingHandlers.fadeVisible = { init: function (element, valueAccessor, allBindingsAccessor, viewModel) {

    var shouldDisplay = valueAccessor(); $(element).toggle(shouldDisplay); }, update: function (element, valueAccessor, allBindingsAccessor, viewModel) { var shouldDisplay = valueAccessor(), allBindings = allBindingsAccessor(); shouldDisplay ? $(element).fadeIn() : $(element).fadeOut(); } }; Bound DOM element 1 Value passed to the binding 2 All bindings on same element 3 The viewmodel 4