Knockoutjs UG meeting presentation

41
www.dotnet.lv

description

Presentation of march Latvian .Net User Group meeting in Riga.

Transcript of Knockoutjs UG meeting presentation

Page 1: Knockoutjs UG meeting presentation

www.dotnet.lv

Page 2: Knockoutjs UG meeting presentation

Valdis IljuconoksSoftware ArchitectMicrosoft MVP

Geta Latvia, Viiar [email protected]://dotnet.lv/blogs/vi

Page 3: Knockoutjs UG meeting presentation

knockout.js

Page 4: Knockoutjs UG meeting presentation

$(function () { $("#btn").click(function () { $.getJSON("/api/customers").then(function (customers) { $.each(customers, function () { $("<li>").text(this.FirstName + " " + this.LastName).appendTo("#customers"); }); }); });});

Page 5: Knockoutjs UG meeting presentation

$(function () { $("#btn").click(function () { $.getJSON("/api/customers").then(function (customers) { var html = tmpl("cust_tmpl", { customers: customers }); $("#customers").html(html); }); });});

<script type="text/html" id="cust_tmpl"> {{each customers}} <li><a href="/api/customer/${Id}">${FirstName} ${LastName}</a></li> {{/each}}</script>

Page 6: Knockoutjs UG meeting presentation

ObservablesBindingsUtilities

Data featuresPlug-ins

Page 7: Knockoutjs UG meeting presentation

Observables

Page 8: Knockoutjs UG meeting presentation

ObservableObservable Array

Dependent Observables (Computed)

Page 9: Knockoutjs UG meeting presentation

ObservablesFunctions (not all browsers support getters and setters)

// read valuevar val = viewmodel.name();

// set valueviewmodel.name(“Valdis”)

Page 10: Knockoutjs UG meeting presentation

Observable ArrayUse with collections

Detect changes only within an arrayUse knockout array methods (cross-browser)

viewmodel.customers = ko.obserableArray();

viewmodel.customers.push(new customer());

Page 11: Knockoutjs UG meeting presentation

indexOf(item)slice(2, 4)push(item)

pop()unshift(item)

shift()reverse()

sort()remove(item)removeAll()

Page 12: Knockoutjs UG meeting presentation

Dependent Observables (Computed)“this” keyword has to be managed

viewmodel.fullname = ko.computed(function() { return this.name() + “ ” + this.surname();})

Page 13: Knockoutjs UG meeting presentation

ObservablesBindingsUtilities

Data featuresPlug-ins

Page 14: Knockoutjs UG meeting presentation

Bindings

Page 15: Knockoutjs UG meeting presentation

Built-in BindingsCustom bindings

Page 16: Knockoutjs UG meeting presentation

Built-in Bindings

Page 17: Knockoutjs UG meeting presentation

data-bind=“”

value:html:text:css:

style:visible:

template:…

Page 18: Knockoutjs UG meeting presentation

Control-flow bindings

Page 19: Knockoutjs UG meeting presentation

data-bind=“”

foreach:if:

ifnot:with:

Page 20: Knockoutjs UG meeting presentation

Custom bindings(most powerful extension point)

Page 21: Knockoutjs UG meeting presentation

Event binding

Page 22: Knockoutjs UG meeting presentation

addOnEnter: function(model, event) { var keycode = (event.which ? event.which : event.keyCode); if (keycode === 13) { viewModel.addTCustomer(); }

return true;}

data-bind=“event: {keypress: addOnEnter}”

Page 23: Knockoutjs UG meeting presentation

Custom bindingsDefine your own behavior

ko.bindingHandlers.yourBindingName = { init: update:}

Page 24: Knockoutjs UG meeting presentation

ko.bindingHandlers.executeOnEnter = { init: function(element, valueAccessor, allBindingAccessor, viewModel) { var value = valueAccessor(); $(element).keypress(function(event) { var keycode = (event.which ? event.which : event.keyCode); if (keycode === 13) { value.call(viewModel); return false; }

return true; }); }};

data-bind=“executeOnEnter: addCustomer”

Page 25: Knockoutjs UG meeting presentation

ObservablesBindingsUtilities

Data featuresPlug-ins

Page 26: Knockoutjs UG meeting presentation

Utilities

Page 27: Knockoutjs UG meeting presentation

ko.utils.arrayFilter()

Page 28: Knockoutjs UG meeting presentation

ko.utils.arrayFilter()ko.utils.arrayFirst()

ko.utils.arrayForEach()ko.utils.arrayIndexOf()

ko.utils.arrayMap()ko.utils.compareArrays()

ko.utils.unwrapObservable()…

Page 29: Knockoutjs UG meeting presentation

ObservablesBindingsUtilities

Data featuresPlug-ins

Page 30: Knockoutjs UG meeting presentation

Data features

Page 31: Knockoutjs UG meeting presentation

ko.toJS()ko.toJSON()

Page 32: Knockoutjs UG meeting presentation

ObservablesBindingsUtilities

Data featuresPlug-ins

Page 33: Knockoutjs UG meeting presentation

Plug-ins

Page 34: Knockoutjs UG meeting presentation

ko.mapping.*

Page 35: Knockoutjs UG meeting presentation

ObservablesBindingsUtilities

Data featuresPlug-ins

Page 36: Knockoutjs UG meeting presentation

Resources

Page 37: Knockoutjs UG meeting presentation

knockoutjs.comblog.stevensanderson.com

knockmeout.comstackoverflow.com -> “knockoutjs”groups.google.com -> “knockoutjs”

Page 38: Knockoutjs UG meeting presentation

must have to build interactive UI

Page 39: Knockoutjs UG meeting presentation

?

Page 40: Knockoutjs UG meeting presentation

Valdis IljuconoksSoftware ArchitectMicrosoft MVP

Geta Latvia, Viiar [email protected]://dotnet.lv/blogs/vi

Page 41: Knockoutjs UG meeting presentation

www.dotnet.lv