SlideShare ist ein Scribd-Unternehmen logo
1 von 32
Downloaden Sie, um offline zu lesen
Laka tranzicija na HTML5
koristeći MVVM
Radenko Zec, Lanaco
Miroslav Popović, Abacus
Opet tranzicija?
pa dokle više…
Zašto?
MVVM
• John Gossman, Microsoft, 2005.
• WPF, Silverlight - mnoštvo biblioteka
Knockout.js
6+ 2+
Razdvaja poslovnu logiku i UI
Declarative bindings
Observables
Knockout u 3 koraka
<input data-bind="value: firstName" />
var viewModel = {
firstName: ko.observable("Radenko")
};
ko.applyBindings(viewModel);
DEMO
Observable
• ko.observable();
• Dvosmjerni binding
• Ručno praćenje izmjena sa subscribe
Computed
• ko.computed();
• Observable čija vrijednost zavisi od drugih
property-a
ObservableArray
• ko.observableArray();
• Prate se izmjene niza (dodavanje i uklanjanje
elemenata)
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>
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
Tok – Control flow
• foreach, if, ifnot, with
<ul data-bind="foreach: items">
<li>Price:
<span data-bind="text: price"></span>
</li>
<ul>
Tok - Containerless Syntax
<ul>
<!-- ko foreach: items -->
<li>Price:
<span data-bind="text: price"></span>
</li>
<!-- /ko -->
<ul>
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>
DEMO
Proširivost
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>
DEMO
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]+$' });
JSON
• ko.toJS, ko.toJSON
• ajax pozivi
$.ajax({ url: '...', data: ko.toJS(this) });
• debugging
data-bind="text: ko.toJSON($root)"
Podaci sa servera
• var serverModel =
@Html.Raw(Json.Encode(Model));
• ASP.NET MVC (Pascal case)
=> JavaScript (Camel case)
• Json.NET, ServiceStack.JsonSerializer
Praćenje promjena
this.dirtyFlag = new ko.dirtyFlag(this);
<button data-bind=“
click: save, enable: dirtyFlag.isDirty()">
Save
</button>
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);}
});
Za kraj…
• http://knockoutjs.com/
• http://knockmeout.net/
• ... za nastavak
Durandal - http://durandaljs.com/
MsNetwork2013 Easy transition to HTML 5 using MVVM
MsNetwork2013 Easy transition to HTML 5 using MVVM

Weitere ähnliche Inhalte

Mehr von Radenko Zec

Introduction to Azure DocumentDB
Introduction to Azure DocumentDBIntroduction to Azure DocumentDB
Introduction to Azure DocumentDBRadenko Zec
 
Developing advanced windows phone apps
Developing advanced windows phone appsDeveloping advanced windows phone apps
Developing advanced windows phone appsRadenko Zec
 
Windays12 ASP.NET Web API
Windays12 ASP.NET Web APIWindays12 ASP.NET Web API
Windays12 ASP.NET Web APIRadenko Zec
 
Sinergija2013 ASP.NET Web API to the max
Sinergija2013 ASP.NET Web API to the maxSinergija2013 ASP.NET Web API to the max
Sinergija2013 ASP.NET Web API to the maxRadenko Zec
 
Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API
Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web APISinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API
Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web APIRadenko Zec
 
MsNetwork2011 -How to build extensible silverlight application using MEF
MsNetwork2011 -How to build extensible silverlight application using MEFMsNetwork2011 -How to build extensible silverlight application using MEF
MsNetwork2011 -How to build extensible silverlight application using MEFRadenko Zec
 
MsCommunity2013- Single page application case study - Silverreader
MsCommunity2013-  Single page application case study - SilverreaderMsCommunity2013-  Single page application case study - Silverreader
MsCommunity2013- Single page application case study - SilverreaderRadenko Zec
 
MsCommunity2012 - Developing REST API using ASP.NET Web API
MsCommunity2012 - Developing REST API using ASP.NET Web APIMsCommunity2012 - Developing REST API using ASP.NET Web API
MsCommunity2012 - Developing REST API using ASP.NET Web APIRadenko Zec
 

Mehr von Radenko Zec (8)

Introduction to Azure DocumentDB
Introduction to Azure DocumentDBIntroduction to Azure DocumentDB
Introduction to Azure DocumentDB
 
Developing advanced windows phone apps
Developing advanced windows phone appsDeveloping advanced windows phone apps
Developing advanced windows phone apps
 
Windays12 ASP.NET Web API
Windays12 ASP.NET Web APIWindays12 ASP.NET Web API
Windays12 ASP.NET Web API
 
Sinergija2013 ASP.NET Web API to the max
Sinergija2013 ASP.NET Web API to the maxSinergija2013 ASP.NET Web API to the max
Sinergija2013 ASP.NET Web API to the max
 
Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API
Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web APISinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API
Sinergija2012 - Developing REST API for Windows Azure with ASP.NET Web API
 
MsNetwork2011 -How to build extensible silverlight application using MEF
MsNetwork2011 -How to build extensible silverlight application using MEFMsNetwork2011 -How to build extensible silverlight application using MEF
MsNetwork2011 -How to build extensible silverlight application using MEF
 
MsCommunity2013- Single page application case study - Silverreader
MsCommunity2013-  Single page application case study - SilverreaderMsCommunity2013-  Single page application case study - Silverreader
MsCommunity2013- Single page application case study - Silverreader
 
MsCommunity2012 - Developing REST API using ASP.NET Web API
MsCommunity2012 - Developing REST API using ASP.NET Web APIMsCommunity2012 - Developing REST API using ASP.NET Web API
MsCommunity2012 - Developing REST API using ASP.NET Web API
 

MsNetwork2013 Easy transition to HTML 5 using MVVM

  • 1.
  • 2. Laka tranzicija na HTML5 koristeći MVVM Radenko Zec, Lanaco Miroslav Popović, Abacus
  • 3.
  • 4.
  • 7. MVVM • John Gossman, Microsoft, 2005. • WPF, Silverlight - mnoštvo biblioteka
  • 8.
  • 9. Knockout.js 6+ 2+ Razdvaja poslovnu logiku i UI Declarative bindings Observables
  • 10. Knockout u 3 koraka <input data-bind="value: firstName" /> var viewModel = { firstName: ko.observable("Radenko") }; ko.applyBindings(viewModel);
  • 11. DEMO
  • 12. Observable • ko.observable(); • Dvosmjerni binding • Ručno praćenje izmjena sa subscribe
  • 13. Computed • ko.computed(); • Observable čija vrijednost zavisi od drugih property-a
  • 14. ObservableArray • ko.observableArray(); • Prate se izmjene niza (dodavanje i uklanjanje elemenata)
  • 15. 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>
  • 16. 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
  • 17. Tok – Control flow • foreach, if, ifnot, with <ul data-bind="foreach: items"> <li>Price: <span data-bind="text: price"></span> </li> <ul>
  • 18. Tok - Containerless Syntax <ul> <!-- ko foreach: items --> <li>Price: <span data-bind="text: price"></span> </li> <!-- /ko --> <ul>
  • 19. 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>
  • 20. DEMO
  • 22. 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>
  • 23. DEMO
  • 24. 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]+$' });
  • 25.
  • 26. JSON • ko.toJS, ko.toJSON • ajax pozivi $.ajax({ url: '...', data: ko.toJS(this) }); • debugging data-bind="text: ko.toJSON($root)"
  • 27. Podaci sa servera • var serverModel = @Html.Raw(Json.Encode(Model)); • ASP.NET MVC (Pascal case) => JavaScript (Camel case) • Json.NET, ServiceStack.JsonSerializer
  • 28. Praćenje promjena this.dirtyFlag = new ko.dirtyFlag(this); <button data-bind=“ click: save, enable: dirtyFlag.isDirty()"> Save </button>
  • 29. 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);} });
  • 30. Za kraj… • http://knockoutjs.com/ • http://knockmeout.net/ • ... za nastavak Durandal - http://durandaljs.com/