HQ Dev Team: Ala Gamal Al-Shargabi •Ivan Peh
KnockOutJS vs AngularJS
How the topic
come around…
SecureSite
(KnockoutJS – 2.1)
HQ
(recently migrated to
Angular 1.3)
We want to share it
with you!
How it all begin…
Before we start..
SIMILARITIES
Both are great JS library to
create rich and responsive UI.
Both can be integrated
reasonably easy with 3rd party
components/frameworks
Screwdriver
VS
Tool box
A data-binding solution
framework which will
allow the application
structure to be entirely up
to you.
A full fledged framework,
which allows you to build a
whole web application with
Angular and only Angular.
As it offers a lot of built-in
features.
COMPARING FEATURES
Data Binding
Let’s have a look at how data binding is done for both libraries, we
will assume that out underlying model is this:
Data Binding
Let’s see how to declare and apply bindings in
HTML using both libraries:
Data Binding
SHUT UP &
SHOW ME THE CODE!!
Fine Fine...Jeez!
Validation
If there’s one thing that
takes probably most of the
work when building forms,
it’s validation.
Form validation
Angular
The usual
suspects -
Attributes
Angular
Input type
Built-In Form Validation
Built-in validations are nice, but in
some cases we need validations that
go far beyond the basic functionality
we get out of the box.
And this is where custom validations
come in.
PRIOR TO 1.3
HALF – BAKED ??
$formatters and $parsers
In versions before 1.3, we were able to
implement custom validations by using:
$formatters and $parsers
Designed to convert the view value
into a different model value.
for converting the model value into
the appropriate view value.
What ??
Model
HTML
View
CSS
Controller
Browser
Example
Angular internally handles date
based input elements.
The view value (entered by the
user) is a string based value,
but when it reaches the scope
the value itself is an instance
of Date.
parser Example
Example
Formatters work in the opposite
way. So if we wanted to convert a
date to a properly formatted
string.
So, it is a model to view update.
formatter Example
Meet the $validators pipeline
Angular 1.3 introduces yet another pipeline, the
$validators pipeline, which is rather used than $parsers
+ $formatters.
Unlike parsers and formatters, the validators pipeline
has access to both, viewValue and modelValue, once
$parsers and $formatters has been successfully run.
The validation pipeline
has been added to the
existing pipeline. It is
basically there, so
developers can explicitly
distinguish between
validations and
parsing/formatting related
functionality.
$validators is not an array,
but an object with each
member describing a
validator.
An Improvement
ASYNCHRONOUS VALIDATION
With 1.3, Angular makes asynchronous validations
possible.
For e.g. Imagine the case you have an input field
for a user name and whenever a user types in a
name, you need to perform some validity checks
on your server. The application needs to wait until
the server responses.
$asyncValidators
Asynchronous validators
work pretty much like
synchronous validators
except that they are
asynchronous and therefore
promise based.
Instead of returning
true or false, we return
a promise that holds the
state of an asynchronous
code execution.
When the promise fulfills itself then the validation is
successful and when it rejects then a validation error is
registered.
Keep in MIND!!
Asynchronous validations
will NOT run unless all of
the prior normal
validators (the validators
present inside of
ngModel.$validators)
have passed.
One thing …
This restriction allows for
the developer (yes you)
to prevent the validator
from making excessive
backend calls when the
input data is invalid.
validators Example
Only once all of the
normal validators and
the async validators
have successfully
passed then the model
value will be written to
the scope.
YES! YES! YES!
ANGULARJS ?
OUR TAKE
• Pure SPA is evil.
• Being the screwdriver that is it,
KnockOut has a lot of bundled
plugins to support the missing parts
of the puzzle.
• KnockOut has higher learning curve
(blessing in disguise).
• KnockOut is great for fast
development from scratch.
•Angular has much more testability.
•More structured and best Practices
mainly due to wider adoption.
• Angular breaks change.
Question ? ?
THANK YOU

#speakgeek - Angular JS

  • 1.
    HQ Dev Team:Ala Gamal Al-Shargabi •Ivan Peh KnockOutJS vs AngularJS
  • 2.
    How the topic comearound… SecureSite (KnockoutJS – 2.1) HQ (recently migrated to Angular 1.3) We want to share it with you!
  • 3.
    How it allbegin…
  • 4.
  • 5.
  • 6.
    Both are greatJS library to create rich and responsive UI. Both can be integrated reasonably easy with 3rd party components/frameworks
  • 7.
    Screwdriver VS Tool box A data-bindingsolution framework which will allow the application structure to be entirely up to you. A full fledged framework, which allows you to build a whole web application with Angular and only Angular. As it offers a lot of built-in features.
  • 8.
  • 9.
    Data Binding Let’s havea look at how data binding is done for both libraries, we will assume that out underlying model is this:
  • 10.
    Data Binding Let’s seehow to declare and apply bindings in HTML using both libraries:
  • 11.
  • 12.
    SHUT UP & SHOWME THE CODE!! Fine Fine...Jeez!
  • 13.
    Validation If there’s onething that takes probably most of the work when building forms, it’s validation. Form validation
  • 14.
  • 15.
  • 16.
    Built-In Form Validation Built-invalidations are nice, but in some cases we need validations that go far beyond the basic functionality we get out of the box. And this is where custom validations come in.
  • 17.
    PRIOR TO 1.3 HALF– BAKED ??
  • 18.
    $formatters and $parsers Inversions before 1.3, we were able to implement custom validations by using: $formatters and $parsers Designed to convert the view value into a different model value. for converting the model value into the appropriate view value.
  • 19.
  • 20.
    Example Angular internally handlesdate based input elements. The view value (entered by the user) is a string based value, but when it reaches the scope the value itself is an instance of Date. parser Example
  • 21.
    Example Formatters work inthe opposite way. So if we wanted to convert a date to a properly formatted string. So, it is a model to view update. formatter Example
  • 22.
    Meet the $validatorspipeline Angular 1.3 introduces yet another pipeline, the $validators pipeline, which is rather used than $parsers + $formatters. Unlike parsers and formatters, the validators pipeline has access to both, viewValue and modelValue, once $parsers and $formatters has been successfully run.
  • 23.
    The validation pipeline hasbeen added to the existing pipeline. It is basically there, so developers can explicitly distinguish between validations and parsing/formatting related functionality. $validators is not an array, but an object with each member describing a validator. An Improvement
  • 25.
  • 26.
    With 1.3, Angularmakes asynchronous validations possible. For e.g. Imagine the case you have an input field for a user name and whenever a user types in a name, you need to perform some validity checks on your server. The application needs to wait until the server responses.
  • 28.
    $asyncValidators Asynchronous validators work prettymuch like synchronous validators except that they are asynchronous and therefore promise based. Instead of returning true or false, we return a promise that holds the state of an asynchronous code execution.
  • 29.
    When the promisefulfills itself then the validation is successful and when it rejects then a validation error is registered.
  • 30.
    Keep in MIND!! Asynchronousvalidations will NOT run unless all of the prior normal validators (the validators present inside of ngModel.$validators) have passed.
  • 31.
    One thing … Thisrestriction allows for the developer (yes you) to prevent the validator from making excessive backend calls when the input data is invalid. validators Example Only once all of the normal validators and the async validators have successfully passed then the model value will be written to the scope.
  • 32.
  • 33.
  • 34.
    • Pure SPAis evil. • Being the screwdriver that is it, KnockOut has a lot of bundled plugins to support the missing parts of the puzzle. • KnockOut has higher learning curve (blessing in disguise). • KnockOut is great for fast development from scratch.
  • 35.
    •Angular has muchmore testability. •More structured and best Practices mainly due to wider adoption. • Angular breaks change.
  • 36.
  • 37.