ASP.NET MVC - Knockoutjs and MVVM

33
KnockoutJS and MVVM @d_danailov

Transcript of ASP.NET MVC - Knockoutjs and MVVM

Page 4: ASP.NET MVC - Knockoutjs and MVVM
Page 5: ASP.NET MVC - Knockoutjs and MVVM
Page 6: ASP.NET MVC - Knockoutjs and MVVM
Page 7: ASP.NET MVC - Knockoutjs and MVVM

Topics Today

● What is MVVM● Why use MVVM● Knockout.js

Page 8: ASP.NET MVC - Knockoutjs and MVVM
Page 9: ASP.NET MVC - Knockoutjs and MVVM

2005

Page 10: ASP.NET MVC - Knockoutjs and MVVM

Photo Credit:http://upload.wikimedia.org/wikipedia/commons/8/87/MVVMPattern.png

Page 11: ASP.NET MVC - Knockoutjs and MVVM

What is MVVM

● Model - View - ModelView● Model = Data Class● View = User Interface● ViewModel (VM)

○ UI binds to this class○ VM consumes services of Model○ VM May expose services/properties of Model

Page 12: ASP.NET MVC - Knockoutjs and MVVM

What is MVVM

● View knows nothing about Model● Model knows nothing about View● Model knows nothing about View Model● View Model knows nothing about View● Communication from View to VM is via data binding

○ Lists for List Boxes and Grids○ IsEnabled properties for Buttons○ Data for Text Boxes

Page 13: ASP.NET MVC - Knockoutjs and MVVM

Why use MVVM

● Separate UI / Business / Data Logic ● Be able to swap out UI

○ Leave business rules/data logic in place○ ASP.NET to Silverlight○ WPF to Windows Phone○ etc.

● Unit testing● Write less UI code

Page 14: ASP.NET MVC - Knockoutjs and MVVM

Photo Credit:http://s.allacronyms.com/2168932girl.png

Page 15: ASP.NET MVC - Knockoutjs and MVVM
Page 16: ASP.NET MVC - Knockoutjs and MVVM

Rich - client - side interactivity

What is KnockoutJS

MVVM pattern

Web Browser Support

Page 17: ASP.NET MVC - Knockoutjs and MVVM

Observable

Core Observable Types

Page 18: ASP.NET MVC - Knockoutjs and MVVM

Observable

Core Observable Types

Computed

Observable Array

Page 19: ASP.NET MVC - Knockoutjs and MVVM

Observable

Core Observable Types

Computed

Observable Array

Page 20: ASP.NET MVC - Knockoutjs and MVVM

Knockout in 3 steps

<input data-bind="value: code" placeholder="code" />

Declarative Binding

Page 21: ASP.NET MVC - Knockoutjs and MVVM

Knockout in 3 stepsvar myViewModel = {

firstName: ko.observable('John')}

Create an Observable

Page 22: ASP.NET MVC - Knockoutjs and MVVM

Knockout in 3 stepsko.applyBindings(myViewModel);

Bind the ViewModelto the View

Page 23: ASP.NET MVC - Knockoutjs and MVVM

How knockout's observable works

Changes to Source notify targets

Page 24: ASP.NET MVC - Knockoutjs and MVVM

How knockout's observable works

Changes to Source notify targets

Changes to targets Notify Source

Page 25: ASP.NET MVC - Knockoutjs and MVVM

How knockout's observable works

Changes to Source notify targets

Changes to targets Notify Source

Two - Way Data Binding

Page 26: ASP.NET MVC - Knockoutjs and MVVM

How knockout's observable works

Changes to Source notify targets

Changes to targets Notify Source

Two - Way Data Binding Event Binding

Page 27: ASP.NET MVC - Knockoutjs and MVVM
Page 28: ASP.NET MVC - Knockoutjs and MVVM
Page 29: ASP.NET MVC - Knockoutjs and MVVM
Page 30: ASP.NET MVC - Knockoutjs and MVVM

MVVM Summary

● MVVM is easy● Just need an additional class● Helps to understand the XAML data binding model● Great for Testing and re-usability● Don’t worry about being “100% code behind free”

Page 32: ASP.NET MVC - Knockoutjs and MVVM