Rich Internet Applications 7. Single-page application architecture.

Slides:



Advertisements
Similar presentations
Developing HTML5 Application using MVVM pattern Pekka Ylenius.
Advertisements

Another look at Knockout JS By Peter Messenger Senior Developer – Triple Point Technology
Knockoutjs and JQuery The One-Two Punch for Richer MVC 4 Applications Connecticut.NET Developers Group November 13, 2012.
Fabian Vilers Hands on ASP.NET MVC.
Languages for Dynamic Web Documents
Copyright 2004 Monash University IMS5401 Web-based Systems Development Topic 2: Elements of the Web (g) Interactivity.
AJAX & By – Anupama Sharma. Defining Ajax Ajax isn’t a technology. It’s really several technologies, each flourishing in its own right, coming together.
Software Architecture Patterns (2). what is architecture? (recap) o an overall blueprint/model describing the structures and properties of a "system"
Chapter 11 ASP.NET JavaScript, Third Edition. 2 Objectives Learn about client/server architecture Study server-side scripting Create ASP.NET applications.
Computer Science 101 Web Access to Databases Overview of Web Access to Databases.
Simon Amrein Consultant Trivadis AG What is ASP.NET 4.0 Ajax Ajax Control Toolkit Microsoft Ajax Content Delivery Network jQuery jQuery Client Templates.
Christopher M. Pascucci Basic Structural Concepts of.NET Browser – Server Interaction.
UNIT-V The MVC architecture and Struts Framework.
Client/Server Architectures
Windows.Net Programming Series Preview. Course Schedule CourseDate Microsoft.Net Fundamentals 01/13/2014 Microsoft Windows/Web Fundamentals 01/20/2014.
NextGen Technology upgrade – Synerizip - Sandeep Kamble.
FALL 2005CSI 4118 – UNIVERSITY OF OTTAWA1 Part 4 Web technologies: HTTP, CGI, PHP,Java applets)
Architecture Of ASP.NET. What is ASP?  Server-side scripting technology.  Files containing HTML and scripting code.  Access via HTTP requests.  Scripting.
A Scalable Application Architecture for composing News Portals on the Internet Serpil TOK, Zeki BAYRAM. Eastern MediterraneanUniversity Famagusta Famagusta.
SUNY Polytechnic Institute CS 490 – Web Design, AJAX, jQueryAngularJS AngularJS is a client-side JavaScript Framework for adding interactivity to HTML.
JavaScript & jQuery the missing manual Chapter 11
CIS 375—Web App Dev II ASP.NET 2 Introducing Web Forms.
Copyright © cs-tutorial.com. Introduction to Web Development In 1990 and 1991,Tim Berners-Lee created the World Wide Web at the European Laboratory for.
XForms: A case study Rajiv Shivane & Pavitar Singh.
CSCI 6962: Server-side Design and Programming Introduction to AJAX.
Chapter 1: Introduction to Web
Server-side Scripting Powering the webs favourite services.
ASP.NET + Ajax Jesper Tørresø ITNET2 F08. Ajax Ajax (Asynchronous JavaScript and XML) A group of interrelated web development techniques used for creating.
NOTE: To change the image on this slide, select the picture and delete it. Then click the Pictures icon in the placeholder to insert your own image. WEB.
Overview of Previous Lesson(s) Over View  ASP.NET Pages  Modular in nature and divided into the core sections  Page directives  Code Section  Page.
JDeveloper 10g and JavaServer Faces: High-Performance UIs on the Web Avrom Roy-Faderman Senior Programmer May, 2006.
CNIT 133 Interactive Web Pags – JavaScript and AJAX JavaScript Environment.
Building Web Sites with ASP.NET MVC Framework Noam King CTO Sela College
Web Programming: Client/Server Applications Server sends the web pages to the client. –built into Visual Studio for development purposes Client displays.
Lecture 19 Web Application Frameworks Boriana Koleva Room: C54
Client Scripting1 Internet Systems Design. Client Scripting2 n “A scripting language is a programming language that is used to manipulate, customize,
ASP.NET.. ASP.NET Environment ASP.NET is Microsoft's programming framework that enables the development of Web applications and services. It is an easy.
JSF Introduction Copyright © Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in print without written permission.
Kingdom of Saudi Arabia Ministry of Higher Education Al-Imam Muhammad Ibn Saud Islamic University College of Computer and Information Sciences Chapter.
Introducing ASP.NET 2.0. Internet Technologies WWW Architecture Web Server Client Server Request Response Network HTTP TCP/IP PC/Mac/Unix + Browser (IE,
Unleash the Power of jQuery Learning & Development Team Telerik Software Academy.
Canopy walk through Single-Page Apps (SPAs) Benjamin Howarth Freelancer, Code Gecko Umbraco UK Festival, Fri 30 th Oct 2015 CODE GECKO.
JavaScript Introduction. Slide 2 Lecture Overview JavaScript background The purpose of JavaScript A first JavaScript example Introduction to getElementById.
Web Design and Development. World Wide Web  World Wide Web (WWW or W3), collection of globally distributed text and multimedia documents and files 
Weekend MS CS Program Internet and Web Technologies COT 5930 Web Project Development - Ajax Dr. Roy Levow, Associate Chair & Professor
Scripting Languages Client Side and Server Side. Examples of client side/server side Examples of client-side side include: JavaScript Jquery (uses a JavaScript.
The basics of knowing the difference CLIENT VS. SERVER.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
AJAX and REST. Slide 2 What is AJAX? It’s an acronym for Asynchronous JavaScript and XML Although requests need not be asynchronous It’s not really a.
Understanding JavaScript and Coding Essentials Lesson 8.
Text INTRODUCTION TO ASP.NET. InterComm Campaign Guidelines CONFIDENTIAL Simply Server side language Simplified page development model Modular, well-factored,
The New Face of ASP.NET ASP.NET MVC, Razor, and jQuery Ido Flatow | Senior Architect | Sela | This session is.
Martin Kruliš by Martin Kruliš (v1.0)1.
//liveVirtualacademy2011/ What’s New for ASP.NET 4.5 and Web Development in Visual Studio 11 Developer Preview Γιώργος Καπνιάς MVP, MCT, MCDP, MCDBA, MCTS,
© Copyright SELA software & Education Labs Ltd Baruch Hirsch St.Bnei Brak Israel
National College of Science & Information Technology.
Jim Fawcett CSE686 – Internet Programming Spring 2014
AJAX and REST.
Model View Controller
AJAX.
Haritha Dasari Josue Balandrano Coronel -
Top Reasons to Choose Angular. Angular is well known for developing robust and adaptable Single Page Applications (SPA). The Application structure is.
jQuery The Easy JavaScript Nikolay Chochev Technical Trainer
Modern web applications
Modern web applications
Serpil TOK, Zeki BAYRAM. Eastern MediterraneanUniversity Famagusta
Web Development Using ASP .NET
DR. JOHN ABRAHAM PROFESSOR UTPA
Web Technologies Computing Science Thompson Rivers University
Web Client Side Technologies Raneem Qaddoura
Presentation transcript:

Rich Internet Applications 7. Single-page application architecture

Presentation layer In a “traditional” web application, presentation layer is within web tier, renders output to client tier (browser) Rich Internet Applications7. Single-page application architecture #2

Presentation layer In a single-page application, presentation layer moves to client tier May also have an element of business logic in client tier Rich Internet Applications7. Single-page application architecture #3

Client Tier presentation Presentation layer technologies are HTML, JavaScript, CSS Easy to end up with spaghetti code – global variables, functions whose purpose is unclear, etc. Can improve this situation using patterns which have been tried and tested in the presentation layer in the Web Tier Rich Internet Applications6. Ajax functionality #4

Presentation layer patterns Model View Controller (MVC) Widely used and well documented and supported in.NET by ASP.NET MVC framework Model View Presenter (MVP) Largely superseded in ASP.NET by MVC, but basis for… Model View ViewModel (MVVM) Not really a Web Tier pattern, used in desktop (WPF) and web (Silverlight) clients Well documented for those applications Rich Internet Applications6. Ajax functionality #5

Benefits of presentation layer patterns Provide structure for presentation layer Separation of concerns Understandability Maintainability Testability Rich Internet Applications6. Ajax functionality #6

Support for client side presentation development Patterns and documentation Many JavaScript libraries….see next slide Take care - wide range in scope, adoption and support for these Libraries play different roles in supporting development: Framework for implementing patterns, e.g. Backbone.js for MVC, Knockout for MVVM Support for implementing specific functionality within overall architecture, e.g. History.js Rich Internet Applications6. Ajax functionality #7

Many JavaScript libraries the-printable-chart/ the-printable-chart/ Rich Internet Applications6. Ajax functionality #8

Aspects of architecture Identify aspects of developing for the Client Tier presentation layer Patterns Models Data binding Controllers/routing Views/templates Testing Will look at example(s) of use of libraries which support each of these Rich Internet Applications6. Ajax functionality #9

MVC 4 SPA template libraries Includes a set of libraries which can form the basis of a SPA Can choose to use or replace any or all of these Rich Internet Applications6. Ajax functionality #10

MVC pattern Based on pattern which is widely used in ASP.NET MVC, Ruby on Rails, J2EE, PHP… Roles of components similar to but not identical to Web Tier application JavaScriptMVC Backbone.js Sproutcore Rich Internet Applications6. Ajax functionality #11

MVC components Rich Internet Applications6. Ajax functionality #12 The Model refers to the data and business functionality of the application The View is the visual representation of the Model The Controller is a component which responds to user input

MVVM pattern Named by Microsoft, though based on earlier Presentation Model Makes use of two-way binding support and command model of WPF in particular JavaScript implementation of this pattern supported by Knockout.js Rich Internet Applications6. Ajax functionality #13

Models Model is presentation layer’s view of the data associated with service Encapsulates response to an Ajax call or data to be posted Consists of JavaScript object(s) Objects can be created in Ajax callback from data in Ajax response Rich Internet Applications6. Ajax functionality #14

Creating objects Numerous ways to create objects in JavaScript, including: Simple objects and object literals Constructor functions and new Object.create Module pattern Revealing module pattern Rich Internet Applications6. Ajax functionality #15

Models in Knockout Model As defined previously View Model a pure-code representation of all the data and operations on a UI e.g. a list of model items to be displayed and methods to add and remove items Knockout can bind view model properties to UI elements Can also bind view model methods to UI events Rich Internet Applications6. Ajax functionality #16

Simple View Model example View model created with object literal Bind to UI element Set up bindings initially Rich Internet Applications6. Ajax functionality #17 var myViewModel = { personName: 'Bob', personAge: 123 }; The name is ko.applyBindings(myViewModel);

Knockout observables Often useful to update UI as property value changes Not just a one-time binding Need to use Knockout observables to allow changes in model properties to be detected and applied to UI Rich Internet Applications6. Ajax functionality #18 var myViewModel = { personName: ko.observable('Bob'), personAge: ko.observable(123) };

View Model functionality Simple example had no methods and no model More realistic view models may have: Properties which are model objects or collections thereof Methods which use Ajax calls to load or post model objects Methods which are bound to UI events, e.g. mouse clicks, or to changes in properties of the view model itself Rich Internet Applications6. Ajax functionality #19

function ViewModel() { var self = this; self.availablePackages = ko.observableArray([]), self.loadPackages = function () { $.ajax({ type: "GET", url: "/Datasources/Packages", dataType: "json", success: function (json) { self.availablePackages.removeAll(); $.each(json, function (i, item) { self.availablePackages.push( new PackageReference(item.packagename, item.packageID)); }); } }); }, View Model example Rich Internet Applications6. Ajax functionality #20 function PackageReference(packagename, packageID) { this.packagename = packagename; this.packageID = packageID; }; function PackageReference(packagename, packageID) { this.packagename = packagename; this.packageID = packageID; }; view model property view model method model

Business logic in model Business logic usually in web tier, but may be need for some logic on client, e.g. validation e.g. using Knockout.validation library Rich Internet Applications6. Ajax functionality #21 var viewModel = { firstName: ko.observable().extend({ minLength: 2, maxLength: 10 }), lastName: ko.observable().extend({ required: true }), submit: function () { if (viewModel.errors().length >0) { alert('Please check your submission.'); viewModel.errors.showAllMessages(); } } };

Benefits of view model Allows state and behaviour of UI to be encapsulated in an object Object does not contain any code which explicitly refers to UI UI and view model combine through Knockout’s model binding View model can be unit tested in isolation from UI Rich Internet Applications6. Ajax functionality #22

Namespaces Put models into namespace to reduce number of objects and functions that are added to the global scope Avoid possible name clashes Create empty object with name of your “namespace” and add models to that Common to use short name, e.g. Knockout uses ko Rich Internet Applications6. Ajax functionality #23 var my = my || {}; my.ViewModel = function () {…}; Var viewModel = new my.ViewModel(); var my = my || {}; my.ViewModel = function () {…}; Var viewModel = new my.ViewModel();

Controllers MVC pattern has the concept of a controller Controller is the glue between application’s views and models Takes user input and calls a function in response That function may interact with model and cause view to be rendered MVVM does not have controllers – their role is carried out by view model and binding Rich Internet Applications6. Ajax functionality #24

Routing User action in traditional web app results in a request being sent to a URL on the server In ASP.NET MVC, request is routed to controller/action In Ajax app, user action can also be expressed as URL, e.g. clicking link If URLs use fragment identifiers then client- side code can read these and respond to change of window.hash by calling a function depending on its value Rich Internet Applications6. Ajax functionality #25

Routing In a sense, the controller’s job is simply to route URLs to functions Routing libraries abstract the task of reading the fragment identifier and calling the appropriate method This capability may be built in to framework or be provided by a standalone library Backbone.js has Router object, which was known as Controller in early versions Rich Internet Applications6. Ajax functionality #26

Backbone Router example Rich Internet Applications6. Ajax functionality #27 var AppRouter = Backbone.Router.extend({ routes: { "/posts/:id": "getPost", "*actions": "defaultRoute“ }, getPost: function( id ) { alert( "Get post number " + id ); }, defaultRoute: function( actions ){ alert( actions ); } }); var app_router = new AppRouter; Backbone.history.start(); … Post 120 Post 130

Routing with Knockout Knockout uses binding rather than routing However, may want to have page states in single page application defined by unique URLs Can combine Knockout with standalone routing library e.g. Sammy.js, Davis.js (!) Rich Internet Applications6. Ajax functionality #28

Knockout with Sammy Rich Internet Applications6. Ajax functionality #29 function AppViewModel() { var self = this; self.go_to_books = function() { location.hash = "books" }; self.go_to_about = function() { location.hash = "about" }; Sammy(function() { this.get('#books', function() { $("#about").hide(); $("#books").show(); }); this.get('#about', function() { $("#books").hide(); $("#about").show(); }); } Books About data binding routing

Views Interface to the application Logicless HTML fragments managed by the application’s controllers Data needed for view transferred to client by Ajax calls, loaded into models No HTML rendered by server Load data into views by creating DOM elements dynamically or by using templates and/or data binding Rich Internet Applications6. Ajax functionality #30

Templates JavaScript template is an HTML fragment interpolated with template variables Conceptually similar to ASP.NET MVC views except that rendering is done in client View consists of markup which selects templates and specifies data to render Further separation of concerns Templates can potentially be reused Rich Internet Applications6. Ajax functionality #31

Storing templates Various options which are available include, depending on the specific template engine: Inline in the JavaScript Inline in the HTML Inline in a custom script tag Loaded remotely Usually loaded with Ajax call Trade-offs in performance between inline/remote templates Rich Internet Applications6. Ajax functionality #32

Some template engines jQuery.tmpl Note: The jQuery team has decided not to take this plugin past beta… jsViews, jsRender Replacement for jQuery.tmpl, still at early stage of development Mustache.js Knockout templates Built-in capability in Knockout Rich Internet Applications6. Ajax functionality #33

Template examples - Knockout Rich Internet Applications6. Ajax functionality #34 Credits: function MyViewModel() { this.people = [ { name: 'Franklin', credits: 250 }, { name: 'Mario', credits: 5800 } ] } ko.applyBindings(new MyViewModel()); See also sample code on module website

Template example – Knockout with jQuery.tmpl Rich Internet Applications6. Ajax functionality #35 {{each people}} ${name} is ${age} years old {{/each}} var viewModel = { people: ko.observableArray([ { name: 'Rod', age: 123 }, { name: 'Jane', age: 125 }, ]) } ko.applyBindings(viewModel);

Templates and data binding Knockout example used data binding Binds view model to template declaratively Can render jQuery.tmpl templates imperatively with $.tmpl method Need to create template first with $.template method jQuery.tmpl templates are simply strings Rich Internet Applications6. Ajax functionality #36

Template example – jQuery.tmpl Rich Internet Applications6. Ajax functionality #37 var markup = " ${Name} (${ReleaseYear}) "; // Compile the markup as a named template $.template( "movieTemplate", markup ); $.ajax({ dataType: "jsonp", url: moviesServiceUrl, jsonp: "$callback", success: showMovies }); // Within the callback, use.tmpl() to render the data. function showMovies( data ) { $.tmpl( "movieTemplate", data ).appendTo( "#movieList" ); }

Unit testing One of the benefits of structuring the presentation layer is testability Can test components separately, e.g. functionality of view model in Knockout Automated tests Same principles as server-side, e.g. Arrange/Act/Assert Need a client-side unit test framework, e.g. qUnit, Jasmine Rich Internet Applications6. Ajax functionality #38

Using qUnit Rich Internet Applications6. Ajax functionality #39 QUnit Test Suite QUnit Test Suite include CSS and markup for test runner include CSS and markup for test runner

Using qUnit Write tests and open page Rich Internet Applications6. Ajax functionality #40 // test this function function isEven(val) { return val % 2 === 0; } // test code test('isEven()', function() { ok(isEven(0), 'Zero is an even number'); ok(isEven(2), 'So is two'); ok(isEven(-4), 'So is negative four'); ok(!isEven(1), 'One is not an even number'); ok(!isEven(-7), 'Neither is negative seven'); }) qUnit test runner in web page – can use other test runners, e.g. Chutzpah in VS

Using qUnit Tests can include asserts, e.g. ok, equal (equals in earlier versions) Rich Internet Applications6. Ajax functionality #41 test('assertions', function() { equal( 2, 1, 'one equals one'); })

Testing Ajax calls When testing functions which include Ajax calls, need to pause test to allow time for callback to execute Rich Internet Applications6. Ajax functionality #42 asyncTest('asynchronous test', function() { // call function under test // The test is automatically paused setTimeout(function() { // assertion // After the assertion has been called, // continue the test start(); }, 100) }) See also sample code on module website

Mocking Ajax calls Mocking allows known data to be supplied in response to Ajax call under test conditions Repeatable, doesn’t depend on state of data on server Use a library such as mockjax Rich Internet Applications6. Ajax functionality #43

Dependency management Large-scale JavaScript apps may require many library script files to be loaded, sometimes in a specific order Basic way of managing this is simply to add script tags to your page JavaScript module loader can allow the process of loading scripts more manageable and also optimise to reduce requests for files Rich Internet Applications6. Ajax functionality #44

Require.js Allows you to specify that a particular part of your code requires a specific set of modules to be loaded Will load modules asynchronously and execute callback when loaded Rich Internet Applications6. Ajax functionality #45 require(["some/module", "a.js", "b.js"], function(someModule) { //This function will be called when all the dependencies are loaded.. //This callback is optional. });

What’s next Alternatives to JavaScript/Ajax Silverlight, GWT, etc. Rich Internet Applications6. Ajax functionality #46