implementing model view view-model in winjs · 2014-05-22 · •the data of the application...

29
http://www.interknowlogy.com/ IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS Philip Japikse (@skimedic) [email protected] www.skimedic.com/blog Microsoft MVP, ASPInsider, MCSD, MCDBA, CSM, CSP Principal Architect, InterKnowlogy

Upload: others

Post on 22-May-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

http://www.interknowlogy.com/

IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS

Philip Japikse (@skimedic)

[email protected]

www.skimedic.com/blog

Microsoft MVP, ASPInsider, MCSD, MCDBA, CSM, CSP

Principal Architect, InterKnowlogy

Page 2: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

PHIL.TOSTRING()

2

•Principal Architect, InterKnowlogy, Inc.

•http://www.interknowlogy.com

•Microsoft MVP, ASPInsider, MCSD, MCDBA, CSM, CSP

•Founder, Agile Conferences, Inc.

•President, Cincinnati .NET User’s Group

•Co-host, Hallway Conversations

•www.hallwayconversations.com

Page 3: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

THE MODEL VIEW VIEW-MODEL PATTERN

3

Page 4: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

• The data of the application

• (Can be) Optimized for storage

MODELS

5/22/2014 4

Page 5: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

• Accepts Interactions from User

• Returns results of interactions back to

user

VIEWS

5/22/2014 5

Page 6: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

• Façade for individual models

• Transport mechanism for models

VIEW MODEL – JOB 1

5/22/2014 6

Page 7: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

• Process Incoming requests

• Perform changes to the model

VIEW MODEL – JOB 2

5/22/2014 7

Page 8: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

WHY MVVM?

8

Page 9: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

DON’T REPEAT YOURSELF

9

Page 10: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

SEPARATION OF CONCERNS

10

Page 11: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

THE COMMAND PATTERN

11

Page 12: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

ENCAPSULATING LOGIC

12

•Wire commands through markup

•markSupportForProcessing

•Check commands through navigation event

•Check Commands on Invocation/Selection

Page 13: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

THE COMMAND PATTERN IN ACTION

13

WinJS.Namespace.define("Navigation", { Commands: {

ObservableModelNavButtonClicked: function (e) { WinJS.Navigation.navigate("pages/a_observableModel/ObservableModel.html");

}, }, wireNavigation: function (args) {

WinJS.Utilities.markSupportedForProcessing(Navigation.Commands.ObservableModelNavButtonClicked);

},

});----------------------------------------------------------------------------

<button id="nbcObservableModel"data-win-control="WinJS.UI.AppBarCommand"data-win-options="{

onclick:Navigation.Commands.ObservableModelNavButtonClicked}"></button>

Page 14: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

The Command Pattern

14

Page 15: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

OBSERVABLES

15

Page 16: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

OBSERVABLE MODELS

16

•WinJS.Binding.as

•Classes

•this._initObservable

•WinJS.Class.mix / WinJS.Binding.mixin

•Properties over Fields

Page 17: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

USING BINDING.AS

17

/// <reference path="//Microsoft.WinJS.1.0/js/ui.js" />/// <reference path="//Microsoft.WinJS.1.0/js/base.js" />(function () {

“use strict"; WinJS.Namespace.define("ViewModel", WinJS.Binding.as({

contact: {firstName: null,lasName: null,age: null,address: {

number: null,street: null,city: null,

},})

})();

Page 18: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

WINJS CLASSES

18

WinJS.Namespace.define("ObservableClassModel", { Contact: WinJS.Class.define(function (firstName, lastName, age) {

this._initObservable(); var self = this; self.firstName = firstName;

}, { firstName: {

set: function (value) {

this.setProperty("firstName", value);}, get: function () {

return this.getProperty("firstName");} },

}),

});

Page 19: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

BINDING MIXIN

19

WinJS.Class.mix( ObservableClassModel.Contact, WinJS.Binding.mixin, WinJS.Binding.expandProperties(

ObservableClassModel.Contact));

Page 20: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

Observable Models

20

Page 21: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

OBSERVABLE COLLECTIONS

21

“use strict"; WinJS.Namespace.define("VM",WinJS.Binding.as({

ContactList :new WinJS.Binding.List(null, {binding:true}),

})

Page 22: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

OBSERVABLE CALCULATED VALUES

22

WinJS.Namespace.define("VM", WinJS.Binding.as({ ContactData: {

numberOfContacts: null, }

}));

VM.ContactData.ContactList.bind("length", function (newVal){if (newVal) {

ViewModel.ContactData.numberOfContacts = newVal; }

});

Page 23: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

Observable Collections

23

Page 24: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

TWO WAY BINDING

24

Page 25: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

TWO WAY BINDING

25

WinJS.Namespace.define("Binding.Mode", { twoway: WinJS.Binding.initializer(function (source, sourceProps, dest, destProps) {

//set up normal bindingvar result = WinJS.Binding.defaultBind(source, sourceProps, dest, destProps);

dest.onchange = function () { var d = dest[destProps[0]]; var s = source[sourceProps[0]]; if (s !== d) source[sourceProps[0]] = d;

} })

});

//full code sample at http://www.skimedic.com/blog --------------------------------------------------------------------------------------------------------

<input id="editFirstName" type="text" data-win-bind="value:firstName Binding.Mode.twoway"/>

Page 26: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

VALUE CONVERTERS

26

WinJS.Namespace.define("ViewModel.Converters", { defaultIfNull: WinJS.Binding.converter(function (val) {

return val ? val : ""; }), ageCheck: WinJS.Binding.converter(function (val) {

return val && val >= 18 ? “green” : “red”; }),

}); ----------------------------------------------------------

<span style="background-color:white"data-win-bind="style.color:age ViewModel.Converters.ageCheck;

textContent:age ViewModel.Converters.defaultIfNull"></span>

Page 27: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

CUSTOM VALUE CONVERTERS

27

WinJS.Namespace.define("ViewModel.Converters", { ageCheck: WinJS.Binding.initializer(

function (source, sourceProps, dest, destProps) { var setColor = function () {

var element = getObject(dest,destProps); var propToChange = destProps[destProps.length - 1]; if (source.age && source.age >= 18) {

element[propToChange]="green"; }else {

element[propToChange] = "red"; }

}; return WinJS.Binding.bind(source, { age: setColor });

}) });

Page 28: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

Two Way Binding

28

Page 29: IMPLEMENTING MODEL VIEW VIEW-MODEL IN WINJS · 2014-05-22 · •The data of the application •(Can be) Optimized for storage MODELS 5/22/2014 4

CONTACT ME

29

[email protected]

•www.skimedic.com/blog

•www.twitter.com/skimedic

•www.hallwayconversations.com

•www.about.me/skimedic