Martin Kruliš by Martin Kruliš (v1.0)1
Single Page Applications ◦ The application logic runs in the browser Provides more desktop-like user experience HTTP requests are handled asynchronously (and covertly) ◦ Traditional browsing is typically discouraged Handled internally by re-rendering the page ◦ Thin server architecture Data storage, security verifications, … ◦ Disadvantages Application boot - loading and initialization time Less stable execution environment (many browser types) by Martin Kruliš (v1.0)2
SPA vs. Desktop Applications ◦ Similar UI concepts Event driven programming ◦ Environment and security It is much simpler to hack code of a web application than regular (compiled) desktop application ◦ Server communication A defining attribute of web applications Another source of runtime errors Asynchronous request may interfere with UI events by Martin Kruliš (v1.0)3
Events and User Actions ◦ Events are entangled with DOM structure ◦ User actions are abstract (not tied to DOM) Event may trigger an action which is not DOM-related ◦ Mechanism for global action processing Components ◦ A smart way how to divide code ◦ Components need to communicate/cooperate ◦ Components may compete for resources Local storage, workers, … by Martin Kruliš (v1.0)4
Model-View-Controller at Client Side by Martin Kruliš (v1.0)5 View Controller Model Process user actions and handles business logic Process user actions and handles business logic Keeps client-side data and manages their sync. with server side (if necessary) Renders data (Javascript structures, JSON, XML, …) into HTML fragments
Model-View-Viewmodel ◦ Simplifies data presentation especially in SPA by Martin Kruliš (v1.0)6 View Model View Model ViewModel Data for business logic Data for presentation Presentation logic Data bindings
AngularJS Library ◦ Open source SPA framework, propelled by Google ◦ Adopts MVC and MVVM patterns Logic in controllers is coded imperatively Data bindings are declarative (embedded in HTML) ◦ $scope object – a context for most operations Variables from scope can be bound to page fragments Two-way data binding Updates are performed iteratively (digest cycle) ◦ Angular 2 recently released Complete rewrite, many changes Microsoft TypeScript – typed version of Javascript by Martin Kruliš (v1.0)7
MVVM and Data Binding ◦ Declarative, embedded in HTML Typically as new attributes and CSS classes Using a special syntax directly in text content User Name: {{ fistName }} {{ lastName }} ◦ The Viewmodel is the $scope context The bindings are bidirectional Automatically propagated in both ways $scope.caption input.value (from above) $scope.firstName -> rendered to the text placeholder The controller works only with the $scope object by Martin Kruliš (v1.0)8
Directives ◦ Many embedded directives for the visual control ng-bind, ng-model ng-if, ng-switch, ng-repeat, ng-show, ng-hide ◦ Defining custom directives Controllers ◦ Perform $scope initialization ◦ Declare functions (actions), which can be triggered by event-handlers (e.g., ng-click ) by Martin Kruliš (v1.0)9 Example 1
MVC and Feature Scalability ◦ The model-view bindings may become troublesome in complex scenarios by Martin Kruliš (v1.0)10 View Model Action Controller Model View
Flux Framework ◦ Dispatcher process one action at a time ◦ Action is propagated to all stores before the views are changed (prevents cycling) ◦ If a view needs to update something, it generates new action by Martin Kruliš (v1.0)11 Action Dispatcher Store View Action
ReactJS Library ◦ Another open source library which uses Flux ◦ Open source, propelled by Facebook and Instagram ◦ Key features Separation of concepts (individual UI components) One-way data flow Child component cannot modify its parent Actions are propagated up through callback Virtual DOM Aggregate changes, so fewer modifications to real DOM are required when a component re-renders itself JSX – Javascript language extension by Martin Kruliš (v1.0)12
Thick Client and Thin Server ◦ HTTP is no longer used (only) in traditional sense ◦ RPC-like concept is used over HTTP or WebSockets ◦ Server performs only those operations that cannot or should not be transferred to client Data management (retrieval, modifications, …) Data are transferred in JSON or in native format (images) Authentication and authorization Trusted computing base cannot be extended to the client Supportive tasks Providing modules, styles, … Rendering fragments of HTML (performance optimization) by Martin Kruliš (v1.0)13
Representational State Transfer (REST) ◦ Server API which offers retrieval and manipulation with application resources in a HTTP-compliant way Resources are identified by URIs Operations are performed by HTTP requests ◦ REST formal constraints are Client-server model Stateless interface (no client context is cached at server) Cacheable (response defines whether it can be cached) Uniform interface Layered system (proxies, servers may be replicated) by Martin Kruliš (v1.0)14
Representational State Transfer (REST) ◦ HTTP request methods reflect desired operations GET – retrieve the resource (nullipotent) POST – append new sub-entity in the resource PUT – insert/replace the resource (idempotent) DELETE – remove the resource (idempotent) ◦ Example API for photo gallery /gallery – collection of all galleries /gallery/kittens - photos in gallery with ID=kittens /gallery/kittens/kitten01 – photo kitten by Martin Kruliš (v1.0)15
REST Example by Martin Kruliš (v1.0)16 /gallery (collection of galleries) /gallery/kittens (photos in gallery) …/kitten01 (single photo) GET Get the list of all galleries (JSON) Get the list of photos in the gallery (JSON) Get the image (jpeg) POST Create a new gallery Create a new photo in a gallery Not generally used. Perhaps for adding image metadata… PUT Replace list of galleries (atypical) Replace entire list of photos in gallery Replace/insert an image (of given ID) DELETE Empty the whole application Remove all photos of a gallery Remove the given image
PHP Slim Framework Example $app = new \Slim\App; $app->get('/hello/{name}', function (Request $request, Response $response) { $name = $request->getAttribute('name'); $response->getBody()->write("Hello, $name"); return $response; }); $app->run(); by Martin Kruliš (v1.0)17
Deployment ◦ New versions are not loaded immediately ◦ Backward compatibility, versions in file names, … Error Handling ◦ Careful error/exception handling ◦ Explicit transfers to server (which may also fail) ◦ Client-side error cache Dependencies in development ◦ Typical SPA uses many libraries ◦ Library management systems exist Composer, npm, Bower, … by Martin Kruliš (v1.0)18
by Martin Kruliš (v1.0)19