39
MVVM for the Web Angular vs. Knockout VS . @basara t

Mvvm knockout vs angular

Embed Size (px)

DESCRIPTION

A presentation I gave at ALT.NET Melbourne on February 26 2013 : http://www.basarat.com/2013/03/knockout-vs-angularjs-mvvm-frameworks.html

Citation preview

Page 1: Mvvm knockout vs angular

MVVM for the Web

Angular vs. Knockout

VS.@basarat

Page 2: Mvvm knockout vs angular

• Cleaner user interface • (used to be Cleaner code only)

• Designer / Developer separation

Why MVVM?

Page 3: Mvvm knockout vs angular

• Two way DataBinding for properties• With some form of interception options

• Command binding • Templates

•Names change but concepts remain the same.

What makes MVVM Work?

Page 4: Mvvm knockout vs angular

MVVM the Knockout Way

Page 5: Mvvm knockout vs angular

• INotifyPropertyChanged (INPC)• ICommand Interface and Command

Property• DataTemplates / ItemTemplates

How MVVM works for XAML

Page 6: Mvvm knockout vs angular

• By Steve Sanderson.• Works at microsoft.

• Highly inspired and directed by what was already there in XAML world. • Perhaps to ease XAML people into HTML.

Enter Knockoutjs

Page 7: Mvvm knockout vs angular

Knockout ViewModel similar to INPC

ko.obervable

ko.computed

ko.obervableArray

Page 8: Mvvm knockout vs angular

Knockout INPC View

• XAML View (dependency properties):

• Knockout View (bindings):

Page 9: Mvvm knockout vs angular

Knockout Commands

• XAML

• Knockout (click and other bindings)

Page 10: Mvvm knockout vs angular

Knockout DataTemplates

• XAML

• knockout

Page 11: Mvvm knockout vs angular

Knockout uses concept of Bindings

Page 12: Mvvm knockout vs angular

Angular js?

Page 13: Mvvm knockout vs angular

• Don’t need them. Means your objects do not need an INPC type of implementation.

• “KO dependency tracking is a clever feature for a problem which angular does not have” – heard on StackOverflow

MVVM with AngularJS : Observables

Page 14: Mvvm knockout vs angular

• Angular remembers the value and compares it to previous value.

• This is basic dirty checking. If there is a change in value, then it fires the change event.

• To know when angular should check the new value it we have scope.

How does it know if anything changed?

Page 15: Mvvm knockout vs angular

Quick AngularJS tutorial

• http://jsfiddle.net/basarat/V9sYB/

Page 16: Mvvm knockout vs angular

• Better syntax, especially for templates• Google plans to make it a web standard so

browsers can do this natively.

• Change listeners fire immediately on setter, which is a problem, since the change listener can further change data, which fires more change events.

Why Angular chose a different method?

Page 17: Mvvm knockout vs angular

Quick AngularJS tutorial 2

• How browsers could support it natively

Page 18: Mvvm knockout vs angular

• Humans are • Slow – anything faster than 50ms is

imperceptible• Limited - can't really show more than about

2000 pieces of information to a human

• Can you do 2000 comparisons in 50 ms even on slow browsers? That means that you have 25us per comparison.

Angular js performance

Page 19: Mvvm knockout vs angular

MVVM the Angular Way

Page 20: Mvvm knockout vs angular

Observables : ViewModel

• https://github.com/basarat/ChessClock• Standard javascript properties your scope• Really good with Typescript

• Angular folks call their framework MV* i.e.

• Model View Whatever

• But like to name it Controller

Page 21: Mvvm knockout vs angular

Observables : The view

• {{property}} syntax

Page 22: Mvvm knockout vs angular

Filters

• Similar to XAML IValueConverter but One way binding for display

Page 23: Mvvm knockout vs angular

Commands

• The VM

• The View

Page 24: Mvvm knockout vs angular

Templates

Page 25: Mvvm knockout vs angular

Angular JS uses concept of directives

Page 26: Mvvm knockout vs angular

Performance

Which one should you use?

Page 27: Mvvm knockout vs angular

jsPrefhttp://jsperf.com/angularjs-vs-knockoutjs

Page 28: Mvvm knockout vs angular

Browser Support

Which one should you use?

Page 29: Mvvm knockout vs angular

• http://docs.angularjs.org/misc/faq • We run our extensive test suite against the

following browsers: Safari, Chrome, Firefox, Opera, IE8, IE9 and mobile browsers (Android, Chrome Mobile, iOS Safari)

• Yahoo’s Class A browser

Angular

Page 30: Mvvm knockout vs angular

• http://knockoutjs.com/documentation/browser-support.html

• Mozilla Firefox 2.0+ (latest version tested = 3.6.8)• Google Chrome (tested on version 5 for Windows and Mac; should

work on older versions too)• Microsoft Internet Explorer 6, 7, 8, 9, 10• Apple Safari (tested on Windows Safari version 5, Mac OS X Safari

version 3.1.2, and iPhone Safari for iOS 4; should work on newer/older versions too)

• Opera 10 for Windows

• Knockout was also found to work on the following browsers (though we don’t recheck these for every release):

• Opera Mini• Google Android OS browser (OS version 2.2)

Knockout

Page 31: Mvvm knockout vs angular

Popularity Contest

Which one should you use?

Page 32: Mvvm knockout vs angular

Knockout vs. Angular

• Google Searches

• StackOverflow tags ( as of feb 2013) • Knockout : 4,304• Angular : 2,794

Page 33: Mvvm knockout vs angular

• Stared on Github (as of feb 2013)• https://github.com/angular/angular.js : 7072 • https://github.com/SteveSanderson/knockout :

3011

Decisive

Page 34: Mvvm knockout vs angular

Debugging

Which one should you use?

Page 35: Mvvm knockout vs angular

Batarang

• Dedicated Debugging tool for angular.

• None for knockout as of yet.

Page 36: Mvvm knockout vs angular

The world really needs both

Conclusion

Page 37: Mvvm knockout vs angular

• Performance : Angular• Legacy Browser (specifically IE) :

Knockout• MS bubble : Knockout• Popularity : Angular• Debugging : Angular• Awesomest Web App Ever : Angular

Choice (my opinion)