32

MsNetwork2013 Easy transition to HTML 5 using MVVM

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: MsNetwork2013 Easy transition to HTML 5 using MVVM
Page 2: MsNetwork2013 Easy transition to HTML 5 using MVVM

Laka tranzicija na HTML5

koristeći MVVM

Radenko Zec, Lanaco

Miroslav Popović, Abacus

Page 3: MsNetwork2013 Easy transition to HTML 5 using MVVM
Page 4: MsNetwork2013 Easy transition to HTML 5 using MVVM
Page 5: MsNetwork2013 Easy transition to HTML 5 using MVVM

Opet tranzicija?pa dokle više…

Page 6: MsNetwork2013 Easy transition to HTML 5 using MVVM

Zašto?

Page 7: MsNetwork2013 Easy transition to HTML 5 using MVVM

MVVM

• John Gossman, Microsoft, 2005.

• WPF, Silverlight - mnoštvo biblioteka

Page 8: MsNetwork2013 Easy transition to HTML 5 using MVVM
Page 9: MsNetwork2013 Easy transition to HTML 5 using MVVM

Knockout.js

6+ 2+

Razdvaja poslovnu logiku i UI

Declarative bindings

Observables

Page 10: MsNetwork2013 Easy transition to HTML 5 using MVVM

Knockout u 3 koraka

<input data-bind="value: firstName" />

var viewModel = {

firstName: ko.observable("Radenko")

};

ko.applyBindings(viewModel);

Page 11: MsNetwork2013 Easy transition to HTML 5 using MVVM

DEMO

Page 12: MsNetwork2013 Easy transition to HTML 5 using MVVM

Observable

• ko.observable();

• Dvosmjerni binding

• Ručno praćenje izmjena sa subscribe

Page 13: MsNetwork2013 Easy transition to HTML 5 using MVVM

Computed

• ko.computed();

• Observable čija vrijednost zavisi od drugih

property-a

Page 14: MsNetwork2013 Easy transition to HTML 5 using MVVM

ObservableArray

• ko.observableArray();

• Prate se izmjene niza (dodavanje i uklanjanje

elemenata)

Page 15: MsNetwork2013 Easy transition to HTML 5 using MVVM

Bindings

• data-bind="..." - HTML5 data atribut

<input type="text“data-bind="enable: canEdit, value: price" />

<select data-bind="options: colors,value: selectedColor,optionsText: 'name',optionsValue: 'key'"></select>

<button data-bind="click: save">OK</button>

Page 16: MsNetwork2013 Easy transition to HTML 5 using MVVM

Knockout bindings

attr checked click css disable enable

event foreach hasfocus html if ifnot

options optionsText optionsValue selectedOptions style submit

template text uniqueName value visible with

Page 17: MsNetwork2013 Easy transition to HTML 5 using MVVM

Tok – Control flow

• foreach, if, ifnot, with

<ul data-bind="foreach: items">

<li>Price:

<span data-bind="text: price"></span>

</li>

<ul>

Page 18: MsNetwork2013 Easy transition to HTML 5 using MVVM

Tok - Containerless Syntax

<ul>

<!-- ko foreach: items -->

<li>Price:

<span data-bind="text: price"></span>

</li>

<!-- /ko -->

<ul>

Page 19: MsNetwork2013 Easy transition to HTML 5 using MVVM

Templates

• Ugrađeni template engine – foreach, if, with…

• template binding (native, jQuery.tmpl)

<div data-bind="template:{ name: 'personTemplate', data: buyer }"></div>

<script type="text/html" id="person-template"><h3 data-bind="text: name"></h3><p>Credits:

<span data-bind="text: credits"></span></p>

</script>

Page 20: MsNetwork2013 Easy transition to HTML 5 using MVVM

DEMO

Page 21: MsNetwork2013 Easy transition to HTML 5 using MVVM

Proširivost

Page 22: MsNetwork2013 Easy transition to HTML 5 using MVVM

Custom Bindings

ko.bindingHandlers.myBinding = { init: function (

element, valueAccessor, allAccessor) {// Poziva se pri prvoj primjeni na element// Postavljanje početnog stanja, event handlera, itd.

},update: function (

element, valueAccessor, allAccessor) {// Prvi put i pri svakoj izmjeni observable vrijednosti// Izmjeniti DOM element ovdje...

}};

<div data-bind="myBinding: value"></div>

Page 23: MsNetwork2013 Easy transition to HTML 5 using MVVM

DEMO

Page 24: MsNetwork2013 Easy transition to HTML 5 using MVVM

Extending Observables

• ko.extenders

• Primjer: Knockout-Validation

firstName: ko.observable().extend({ required: true });

email: ko.observable().extend({ email: true });

username: ko.observable().extend({ pattern: '^[a-z0-9]+$' });

Page 25: MsNetwork2013 Easy transition to HTML 5 using MVVM
Page 26: MsNetwork2013 Easy transition to HTML 5 using MVVM

JSON

• ko.toJS, ko.toJSON

• ajax pozivi

$.ajax({ url: '...', data: ko.toJS(this) });

• debugging

data-bind="text: ko.toJSON($root)"

Page 27: MsNetwork2013 Easy transition to HTML 5 using MVVM

Podaci sa servera

• var serverModel =

@Html.Raw(Json.Encode(Model));

• ASP.NET MVC (Pascal case)

=> JavaScript (Camel case)

• Json.NET, ServiceStack.JsonSerializer

Page 28: MsNetwork2013 Easy transition to HTML 5 using MVVM

Praćenje promjena

this.dirtyFlag = new ko.dirtyFlag(this);

<button data-bind=“

click: save, enable: dirtyFlag.isDirty()">

Save

</button>

Page 29: MsNetwork2013 Easy transition to HTML 5 using MVVM

Update / Revert / Commit pattern

var VM = function (data) {

this.name = ko.observable();

this.cache = function () { };

this.update(data);

};

ko.utils.extend(VM.prototype, {

update: function (data) {

this.name(data.name || '- new -');

this.cache.latestData = data;

},

revert: function () { this.update(this.cache.latestData); },

commit: function () { this.cache.latestData = ko.toJS(this);}

});

Page 30: MsNetwork2013 Easy transition to HTML 5 using MVVM

Za kraj…

• http://knockoutjs.com/

• http://knockmeout.net/

• ... za nastavak

Durandal - http://durandaljs.com/

Page 31: MsNetwork2013 Easy transition to HTML 5 using MVVM
Page 32: MsNetwork2013 Easy transition to HTML 5 using MVVM