SlideShare ist ein Scribd-Unternehmen logo
1 von 30
SINGLE PAGE
APPLICATIONS WITH
JAVASCRIPT AND ASP.NET
MVC4

Author: Yurii Shapovalov
Yurii_Shapovalov@epam.com
TOPICS
 Definition
 SPA Architecture
 Server Stack
 Client Stack
 JavaScript Patterns
 Web Optimization
 Examples of components
WHAT’S A SPA?
• is a web application that fits on a single web page, providing a fluid

UX akin to a desktop application.

• Maintain navigation, history, page linking.
• Whole applications fits on a single web page.
• Persisting important state on the client
• Fully (or mostly) loaded in the initial page load
• Progressively downloads data as required
SPA HIGH LEVEL
ARCHITECTURE
Server

Client

ASP.NET
+ (JavaScript /
HTML / CSS)

Views
(HTML / CSS)

Data Services

Application
(JavaScript)

Database

Data Access
(JavaScript)

Navigation
SERVER SIDE DESIGN
Web API

ASP.NET Web API

Unit of Work Pattern

Repository Pattern

Entity Framework

EF Code First

Database

JSON
ASP.NET WEB API
• Simplifies web services

• Good for serving JSON
JSON
Models

SQL Server

Data Layer

ASP.NET Web
API
CONTROLLERS
public IEnumerable<Activity> GetAll()
{
return Uow.Activities.GetAll().OrderBy(p => p.Name);
}
public Activity Get(int id)
{
var activity = Uow.Activities.GetById(id);
if(activity != null)
return activity;
}
public HttpResponseMessage Put(Activity activity)
{
Uow.Activities.Update(activity);
Uow.Commit();
return new HttpResponseMessage(HttpStatusCode.NoContent);
}}
INDEX.HTML
• Using HTML5 Boilerplate
• (http://html5boilerplate.com/)
• Modernizr.js – checks supported features in client
browser.
• @using System.Web.Optimization
• Configuring App_Start
• BundleConfig
BUNDLING AND MINIFICATION
• To reduce size of resources which we transfer to

client in initial page load, we are using bundling
and minification.
• Bundling reduce server calls for resources.

• Minification reduce the size of file.
BUNDLING
• Instead of this:
<script type="text/javascript" src="jquery.easing.js"></script>
<script type="text/javascript" src="jquery-ui.js"></script>
<script type="text/javascript" src="inputlabel.js"></script>
<script type="text/javascript" src="jquery.treeview.js"></script>
<script type="text/javascript “src="quickpager.jquery.js"></script>
<script type="text/javascript" src="jquery.modalboxmin.js"></script>
<script type="text/javascript" src="jquery.activity-indi.js"></script>
<script type="text/javascript" src="/lib/js/tabs.js"></script>
<script type="text/javascript" src="/lib/js/socializ.js"></script>
<script type="text/javascript" src="/lib/js/muScript.js"></script>
<!-- ... -->
BUNDLING
… doing this for scripts:
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/jsextlibs").Include(
"~/Scripts/lib/jquery.activity-indicator-{version}.js",
"~/Scripts/lib/knockout-{version}.js",
"~/Scripts/lib/underscore.js",
"~/Scripts/lib/moment.js",
"~/Scripts/lib/sammy.js",
"~/Scripts/lib/amplify.js"
// ...
));
BUNDLING
… and for other resources:
bundles.Add(new ScriptBundle("~/bundles/jsapplibs").
IncludeDirectory("~/Scripts/app/", "*.js",
searchSubdirectories: false));

bundles.Add(new StyleBundle("~/Content/css").
Include(
"~/Content/normalize.css",
"~/Content/main.css", ));
bundles.Add(new Bundle("~/Content/Less",
new LessTransform(), new CssMinify())
.Include("~/Content/styles.less"));
BUNDLING
Add bundles on index.html
@Styles.Render("~/Content/css", "~/Content/less")
@Scripts.Render("~/bundles/modernizr")

@Scripts.Render(

"~/bundles/jquery",
"~/bundles/jsextlibs",
"~/Scripts/lib/require.js",
"~/bundles/jsapplibs",

"~/bundles/jsmocks",
"~/Scripts/main.js"
)
BUNDLING AND MINIFICATION
Using B/M

Without B/M

Chang
e

File
Requests

9

34

256%

KB Sent

3.26

11.92

266%

KB
Received

388.51

530

36%

780 MS

53%

Load Time 510 MS
CSS AND LESS
Less – Dynamic Stylesheet language
.accent-top (@sizeMultiplier: 1, @lineHeightMultiplier: 1)
{
.accent;
width: @base-accent-top-width * @sizeMultiplier;
height: @base-accent-top-height * @sizeMultiplier;
div {
text-transform: uppercase;
}}
LESS TO CSS
 Creating custom BundleTransform using library dotLett.

public class LessTransform : IBundleTransform
{
public void Process(BundleContext c, BundleResponse resp)
{

resp.Content = dotless.Core.Less.Parse(resp.Content);
response.ContentType = "text/css";
}}
RENDER VIEWS
• Render all pages in main section.
• All pages are stored in Views folder
• General components like header, navigation and footer
we are defining above or below of main section.

<section class="main">
@RenderPage("Views/workspace.cshtml")
@RenderPage("Views/statistics.cshtml")
@RenderPage("Views/settings.cshtml")
@* ... *@
</section>
VIEWS
• View is a simple html with data-bind.
• Views might contain Knockout templates
<section id="workspace-view" class="view">
<h2 data-bind="text: context.date"></h2>
<ul data-bind="template: { name: 'activity-template',
foreach: activities }" class="activity"></ul>
<button data-bind="click: addActivity" class="addactivity">+</button>
<table data-bind="template: { name: timelogTemplate }">
</table>
</section>
CLIENT SIDE DESIGN
HTML Views
HTML Views
HTML Views
HTML / Views

Bootstrapper
MVVM Binder

HTML / Views
HTML / Views
HTML / Views
ViewModels

Sorting
Filtering

Data Primer

HTML / Views
HTML / Views
HTML / Views
Models

Data Context
Data Services

Model Mappers
Model Mappers
Model Mappers
Model Mappers
JAVASCRIPT LIBRARIES
jQuery

Working with DOM,

Knockout.js

Data Binding and MVVM

Amplify.js

Data Push/Pull, Caching, Client Storage

Breeze.js

Querying with filters, ordering and paging

Sammy.js

Navigation and History

require.js

Dependency Resolution

underscore.js

JavaScript helper

toastr.js

UI Alerts

qunit

Unit Testing
JAVASCRIPT PATTERNS
• AMD (Asynchronous Module Definition)
• is a JavaScript API for defining modules and their
dependencies in such a way that modules may be
asynchronously loaded
• Revealing Module Pattern
• we define all of our functions and variables in the
private scope and return an anonymous object with
pointers to the private functionality we wished to reveal
as public.
• MVVM
AMD PATTERN
Application

Controller

Navigation

Data Services

Data Context

Model

• Sequence of loading component might
cause an issue.
DEFINING A MODULE IN
REQUIRE.JS
Module ID
define('model',

Dependencies

[‘settings', 'model.workspace'],
function (settings, workspace) {
var

model = {
Workspace: workspace
}; // ...
return model;
});

Module Factory
STARTING THE SPA
Bootstrapper

Prime the Data

Setup the presentation

Data Services

HTML Views

Data Context

View Models

Models

Navigation Router
BOOTSTRAPPER
Bootstrapper responsible for initial application run.
define('bootstrapper',
var run = function () {

['jquery', 'route-config', 'presenter', 'dataprimer', 'binder'],
function ($, routeConfig, presenter, dataprimer, binder) {
presenter.toggleActivity(true);
var run = function () {
$.when(dataprimer.fetch())
presenter.toggleActivity(true);
$.when(dataprimer.fetch())
.done(binder.bind)
.done(binder.bind)
.done(routeConfig.register)
.done(routeConfig.register)
.always(function () {
presenter.toggleActivity(false);
.always(function () {
});
}
presenter.toggleActivity(false);
return {
run : run
}}
}});
DATASERVICE
var init = function () {
amplify.request.define('activities', 'ajax', {
url: '/api/activities',
dataType: 'json',
type: 'GET'
}); },
getActivities = function (callbacks) {
return amplify.request({
resourceId: 'activities',
data: '',
success: callbacks.success,
error: callbacks.error
});}; init();
return {
getActivities: getActivities
};
VIEWMODEL
activityItemTemplate = 'common.activityitem',
timelogTemplate = 'common.timelogtable';
activities = ko.observableArray();
activate = function (routerData, callback) {
refresh(callback); }
getWorkspace = function (callback) {
datacontext.activities.getActivities(id);
}
return {
activate: activate
activityItemTemplate: activityItemTemplate,
timelogTemplate: timelogTemplate
}});
AS RESULT WE HAVE
• Async services
• Client side application & business logic

• Long-lived client-side state
• 2 way data binding
• Tombstoning / dropped connection
PROS AND CONS
• Faster UI

• Bad for SEO

• More Interactive

• More complex to build

• Can work offline

• Need to expose
application logic to
client

• UI is just another

Client
• UI can have BI
• Perfect for HTML5

and Mobile apps

• Require strong
JavaScript skills
QUESTIONS?
Yurii Shapovalov
Email: Yurii_Shapovalov@epam.com
Skype: Shapovalov.Yuriy

Weitere ähnliche Inhalte

Was ist angesagt?

Learning ASP.NET 5 and MVC 6
Learning ASP.NET 5 and MVC 6Learning ASP.NET 5 and MVC 6
Learning ASP.NET 5 and MVC 6Ido Flatow
 
Escaping the yellow bubble - rewriting Domino using MongoDb and Angular
Escaping the yellow bubble - rewriting Domino using MongoDb and AngularEscaping the yellow bubble - rewriting Domino using MongoDb and Angular
Escaping the yellow bubble - rewriting Domino using MongoDb and AngularMark Leusink
 
Progressive Web Apps
Progressive Web AppsProgressive Web Apps
Progressive Web AppsKranthi Lakum
 
Modern Web App Architectures
Modern Web App ArchitecturesModern Web App Architectures
Modern Web App ArchitecturesRaphael Stary
 
Single page application
Single page applicationSingle page application
Single page applicationJeremy Lee
 
ASP.NET MVC Performance
ASP.NET MVC PerformanceASP.NET MVC Performance
ASP.NET MVC Performancerudib
 
ASP.NET - Introduction to Web Forms and MVC
ASP.NET - Introduction to Web Forms and MVCASP.NET - Introduction to Web Forms and MVC
ASP.NET - Introduction to Web Forms and MVCBilal Amjad
 
Developing an aspnet web application
Developing an aspnet web applicationDeveloping an aspnet web application
Developing an aspnet web applicationRahul Bansal
 
Introduction to angular js july 6th 2014
Introduction to angular js   july 6th 2014Introduction to angular js   july 6th 2014
Introduction to angular js july 6th 2014Simona Clapan
 
Active Server Page(ASP)
Active Server Page(ASP)Active Server Page(ASP)
Active Server Page(ASP)Keshab Nath
 
ASP.NET MVC and ajax
ASP.NET MVC and ajax ASP.NET MVC and ajax
ASP.NET MVC and ajax Brij Mishra
 
The MEAN stack - SoCalCodeCamp - june 29th 2014
The MEAN stack - SoCalCodeCamp - june 29th 2014The MEAN stack - SoCalCodeCamp - june 29th 2014
The MEAN stack - SoCalCodeCamp - june 29th 2014Simona Clapan
 
Asp .net web form fundamentals
Asp .net web form fundamentalsAsp .net web form fundamentals
Asp .net web form fundamentalsGopal Ji Singh
 
Building single page applications
Building single page applicationsBuilding single page applications
Building single page applicationsSC5.io
 

Was ist angesagt? (20)

Learning ASP.NET 5 and MVC 6
Learning ASP.NET 5 and MVC 6Learning ASP.NET 5 and MVC 6
Learning ASP.NET 5 and MVC 6
 
ASP.NET Web form
ASP.NET Web formASP.NET Web form
ASP.NET Web form
 
Escaping the yellow bubble - rewriting Domino using MongoDb and Angular
Escaping the yellow bubble - rewriting Domino using MongoDb and AngularEscaping the yellow bubble - rewriting Domino using MongoDb and Angular
Escaping the yellow bubble - rewriting Domino using MongoDb and Angular
 
Progressive Web Apps
Progressive Web AppsProgressive Web Apps
Progressive Web Apps
 
Modern Web App Architectures
Modern Web App ArchitecturesModern Web App Architectures
Modern Web App Architectures
 
Single page application
Single page applicationSingle page application
Single page application
 
ASP.NET MVC Performance
ASP.NET MVC PerformanceASP.NET MVC Performance
ASP.NET MVC Performance
 
ASP.NET - Introduction to Web Forms and MVC
ASP.NET - Introduction to Web Forms and MVCASP.NET - Introduction to Web Forms and MVC
ASP.NET - Introduction to Web Forms and MVC
 
Single page application
Single page applicationSingle page application
Single page application
 
Developing an aspnet web application
Developing an aspnet web applicationDeveloping an aspnet web application
Developing an aspnet web application
 
Introduction to angular js july 6th 2014
Introduction to angular js   july 6th 2014Introduction to angular js   july 6th 2014
Introduction to angular js july 6th 2014
 
Active Server Page(ASP)
Active Server Page(ASP)Active Server Page(ASP)
Active Server Page(ASP)
 
ASP.NET MVC and ajax
ASP.NET MVC and ajax ASP.NET MVC and ajax
ASP.NET MVC and ajax
 
Asp.net
 Asp.net Asp.net
Asp.net
 
The MEAN stack - SoCalCodeCamp - june 29th 2014
The MEAN stack - SoCalCodeCamp - june 29th 2014The MEAN stack - SoCalCodeCamp - june 29th 2014
The MEAN stack - SoCalCodeCamp - june 29th 2014
 
Asp .net web form fundamentals
Asp .net web form fundamentalsAsp .net web form fundamentals
Asp .net web form fundamentals
 
Tutorial asp.net
Tutorial  asp.netTutorial  asp.net
Tutorial asp.net
 
Web component
Web componentWeb component
Web component
 
You Know WebOS
You Know WebOSYou Know WebOS
You Know WebOS
 
Building single page applications
Building single page applicationsBuilding single page applications
Building single page applications
 

Ähnlich wie Single Page Applications on JavaScript and ASP.NET MVC4

Writing HTML5 Web Apps using Backbone.js and GAE
Writing HTML5 Web Apps using Backbone.js and GAEWriting HTML5 Web Apps using Backbone.js and GAE
Writing HTML5 Web Apps using Backbone.js and GAERon Reiter
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...Fabio Franzini
 
WordPress as the Backbone(.js)
WordPress as the Backbone(.js)WordPress as the Backbone(.js)
WordPress as the Backbone(.js)Beau Lebens
 
Understanding backbonejs
Understanding backbonejsUnderstanding backbonejs
Understanding backbonejsNick Lee
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenerytoddbr
 
How and why i roll my own node.js framework
How and why i roll my own node.js frameworkHow and why i roll my own node.js framework
How and why i roll my own node.js frameworkBen Lin
 
e-suap - client technologies- english version
e-suap - client technologies- english versione-suap - client technologies- english version
e-suap - client technologies- english versionSabino Labarile
 
OSGi and Spring Data for simple (Web) Application Development
OSGi and Spring Data  for simple (Web) Application DevelopmentOSGi and Spring Data  for simple (Web) Application Development
OSGi and Spring Data for simple (Web) Application DevelopmentChristian Baranowski
 
OSGi and Spring Data for simple (Web) Application Development - Christian Bar...
OSGi and Spring Data for simple (Web) Application Development - Christian Bar...OSGi and Spring Data for simple (Web) Application Development - Christian Bar...
OSGi and Spring Data for simple (Web) Application Development - Christian Bar...mfrancis
 
Dependency Management with RequireJS
Dependency Management with RequireJSDependency Management with RequireJS
Dependency Management with RequireJSAaronius
 
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016Nicolás Bouhid
 
using Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API'susing Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API'sAntônio Roberto Silva
 
Backbone.js — Introduction to client-side JavaScript MVC
Backbone.js — Introduction to client-side JavaScript MVCBackbone.js — Introduction to client-side JavaScript MVC
Backbone.js — Introduction to client-side JavaScript MVCpootsbook
 
Angular 2 Essential Training
Angular 2 Essential Training Angular 2 Essential Training
Angular 2 Essential Training Patrick Schroeder
 
SPA using Rails & Backbone
SPA using Rails & BackboneSPA using Rails & Backbone
SPA using Rails & BackboneAshan Fernando
 
Modular Test-driven SPAs with Spring and AngularJS
Modular Test-driven SPAs with Spring and AngularJSModular Test-driven SPAs with Spring and AngularJS
Modular Test-driven SPAs with Spring and AngularJSGunnar Hillert
 
Headless Drupal en pratique
Headless Drupal en pratiqueHeadless Drupal en pratique
Headless Drupal en pratiqueSimon Morvan
 

Ähnlich wie Single Page Applications on JavaScript and ASP.NET MVC4 (20)

Writing HTML5 Web Apps using Backbone.js and GAE
Writing HTML5 Web Apps using Backbone.js and GAEWriting HTML5 Web Apps using Backbone.js and GAE
Writing HTML5 Web Apps using Backbone.js and GAE
 
WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...WebNet Conference 2012 - Designing complex applications using html5 and knock...
WebNet Conference 2012 - Designing complex applications using html5 and knock...
 
WordPress as the Backbone(.js)
WordPress as the Backbone(.js)WordPress as the Backbone(.js)
WordPress as the Backbone(.js)
 
Understanding backbonejs
Understanding backbonejsUnderstanding backbonejs
Understanding backbonejs
 
Javascript first-class citizenery
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenery
 
How and why i roll my own node.js framework
How and why i roll my own node.js frameworkHow and why i roll my own node.js framework
How and why i roll my own node.js framework
 
e-suap - client technologies- english version
e-suap - client technologies- english versione-suap - client technologies- english version
e-suap - client technologies- english version
 
Liferay (DXP) 7 Tech Meetup for Developers
Liferay (DXP) 7 Tech Meetup for DevelopersLiferay (DXP) 7 Tech Meetup for Developers
Liferay (DXP) 7 Tech Meetup for Developers
 
MVS: An angular MVC
MVS: An angular MVCMVS: An angular MVC
MVS: An angular MVC
 
OSGi and Spring Data for simple (Web) Application Development
OSGi and Spring Data  for simple (Web) Application DevelopmentOSGi and Spring Data  for simple (Web) Application Development
OSGi and Spring Data for simple (Web) Application Development
 
OSGi and Spring Data for simple (Web) Application Development - Christian Bar...
OSGi and Spring Data for simple (Web) Application Development - Christian Bar...OSGi and Spring Data for simple (Web) Application Development - Christian Bar...
OSGi and Spring Data for simple (Web) Application Development - Christian Bar...
 
Dependency Management with RequireJS
Dependency Management with RequireJSDependency Management with RequireJS
Dependency Management with RequireJS
 
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
Ajax on drupal the right way - DrupalCamp Campinas, São Paulo, Brazil 2016
 
using Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API'susing Mithril.js + postgREST to build and consume API's
using Mithril.js + postgREST to build and consume API's
 
Backbone.js — Introduction to client-side JavaScript MVC
Backbone.js — Introduction to client-side JavaScript MVCBackbone.js — Introduction to client-side JavaScript MVC
Backbone.js — Introduction to client-side JavaScript MVC
 
Backbone js
Backbone jsBackbone js
Backbone js
 
Angular 2 Essential Training
Angular 2 Essential Training Angular 2 Essential Training
Angular 2 Essential Training
 
SPA using Rails & Backbone
SPA using Rails & BackboneSPA using Rails & Backbone
SPA using Rails & Backbone
 
Modular Test-driven SPAs with Spring and AngularJS
Modular Test-driven SPAs with Spring and AngularJSModular Test-driven SPAs with Spring and AngularJS
Modular Test-driven SPAs with Spring and AngularJS
 
Headless Drupal en pratique
Headless Drupal en pratiqueHeadless Drupal en pratique
Headless Drupal en pratique
 

Kürzlich hochgeladen

"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 

Kürzlich hochgeladen (20)

"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 

Single Page Applications on JavaScript and ASP.NET MVC4

  • 1. SINGLE PAGE APPLICATIONS WITH JAVASCRIPT AND ASP.NET MVC4 Author: Yurii Shapovalov Yurii_Shapovalov@epam.com
  • 2. TOPICS  Definition  SPA Architecture  Server Stack  Client Stack  JavaScript Patterns  Web Optimization  Examples of components
  • 3. WHAT’S A SPA? • is a web application that fits on a single web page, providing a fluid UX akin to a desktop application. • Maintain navigation, history, page linking. • Whole applications fits on a single web page. • Persisting important state on the client • Fully (or mostly) loaded in the initial page load • Progressively downloads data as required
  • 4. SPA HIGH LEVEL ARCHITECTURE Server Client ASP.NET + (JavaScript / HTML / CSS) Views (HTML / CSS) Data Services Application (JavaScript) Database Data Access (JavaScript) Navigation
  • 5. SERVER SIDE DESIGN Web API ASP.NET Web API Unit of Work Pattern Repository Pattern Entity Framework EF Code First Database JSON
  • 6. ASP.NET WEB API • Simplifies web services • Good for serving JSON JSON Models SQL Server Data Layer ASP.NET Web API
  • 7. CONTROLLERS public IEnumerable<Activity> GetAll() { return Uow.Activities.GetAll().OrderBy(p => p.Name); } public Activity Get(int id) { var activity = Uow.Activities.GetById(id); if(activity != null) return activity; } public HttpResponseMessage Put(Activity activity) { Uow.Activities.Update(activity); Uow.Commit(); return new HttpResponseMessage(HttpStatusCode.NoContent); }}
  • 8. INDEX.HTML • Using HTML5 Boilerplate • (http://html5boilerplate.com/) • Modernizr.js – checks supported features in client browser. • @using System.Web.Optimization • Configuring App_Start • BundleConfig
  • 9. BUNDLING AND MINIFICATION • To reduce size of resources which we transfer to client in initial page load, we are using bundling and minification. • Bundling reduce server calls for resources. • Minification reduce the size of file.
  • 10. BUNDLING • Instead of this: <script type="text/javascript" src="jquery.easing.js"></script> <script type="text/javascript" src="jquery-ui.js"></script> <script type="text/javascript" src="inputlabel.js"></script> <script type="text/javascript" src="jquery.treeview.js"></script> <script type="text/javascript “src="quickpager.jquery.js"></script> <script type="text/javascript" src="jquery.modalboxmin.js"></script> <script type="text/javascript" src="jquery.activity-indi.js"></script> <script type="text/javascript" src="/lib/js/tabs.js"></script> <script type="text/javascript" src="/lib/js/socializ.js"></script> <script type="text/javascript" src="/lib/js/muScript.js"></script> <!-- ... -->
  • 11. BUNDLING … doing this for scripts: public static void RegisterBundles(BundleCollection bundles) { bundles.Add(new ScriptBundle("~/bundles/jsextlibs").Include( "~/Scripts/lib/jquery.activity-indicator-{version}.js", "~/Scripts/lib/knockout-{version}.js", "~/Scripts/lib/underscore.js", "~/Scripts/lib/moment.js", "~/Scripts/lib/sammy.js", "~/Scripts/lib/amplify.js" // ... ));
  • 12. BUNDLING … and for other resources: bundles.Add(new ScriptBundle("~/bundles/jsapplibs"). IncludeDirectory("~/Scripts/app/", "*.js", searchSubdirectories: false)); bundles.Add(new StyleBundle("~/Content/css"). Include( "~/Content/normalize.css", "~/Content/main.css", )); bundles.Add(new Bundle("~/Content/Less", new LessTransform(), new CssMinify()) .Include("~/Content/styles.less"));
  • 13. BUNDLING Add bundles on index.html @Styles.Render("~/Content/css", "~/Content/less") @Scripts.Render("~/bundles/modernizr") @Scripts.Render( "~/bundles/jquery", "~/bundles/jsextlibs", "~/Scripts/lib/require.js", "~/bundles/jsapplibs", "~/bundles/jsmocks", "~/Scripts/main.js" )
  • 14. BUNDLING AND MINIFICATION Using B/M Without B/M Chang e File Requests 9 34 256% KB Sent 3.26 11.92 266% KB Received 388.51 530 36% 780 MS 53% Load Time 510 MS
  • 15. CSS AND LESS Less – Dynamic Stylesheet language .accent-top (@sizeMultiplier: 1, @lineHeightMultiplier: 1) { .accent; width: @base-accent-top-width * @sizeMultiplier; height: @base-accent-top-height * @sizeMultiplier; div { text-transform: uppercase; }}
  • 16. LESS TO CSS  Creating custom BundleTransform using library dotLett. public class LessTransform : IBundleTransform { public void Process(BundleContext c, BundleResponse resp) { resp.Content = dotless.Core.Less.Parse(resp.Content); response.ContentType = "text/css"; }}
  • 17. RENDER VIEWS • Render all pages in main section. • All pages are stored in Views folder • General components like header, navigation and footer we are defining above or below of main section. <section class="main"> @RenderPage("Views/workspace.cshtml") @RenderPage("Views/statistics.cshtml") @RenderPage("Views/settings.cshtml") @* ... *@ </section>
  • 18. VIEWS • View is a simple html with data-bind. • Views might contain Knockout templates <section id="workspace-view" class="view"> <h2 data-bind="text: context.date"></h2> <ul data-bind="template: { name: 'activity-template', foreach: activities }" class="activity"></ul> <button data-bind="click: addActivity" class="addactivity">+</button> <table data-bind="template: { name: timelogTemplate }"> </table> </section>
  • 19. CLIENT SIDE DESIGN HTML Views HTML Views HTML Views HTML / Views Bootstrapper MVVM Binder HTML / Views HTML / Views HTML / Views ViewModels Sorting Filtering Data Primer HTML / Views HTML / Views HTML / Views Models Data Context Data Services Model Mappers Model Mappers Model Mappers Model Mappers
  • 20. JAVASCRIPT LIBRARIES jQuery Working with DOM, Knockout.js Data Binding and MVVM Amplify.js Data Push/Pull, Caching, Client Storage Breeze.js Querying with filters, ordering and paging Sammy.js Navigation and History require.js Dependency Resolution underscore.js JavaScript helper toastr.js UI Alerts qunit Unit Testing
  • 21. JAVASCRIPT PATTERNS • AMD (Asynchronous Module Definition) • is a JavaScript API for defining modules and their dependencies in such a way that modules may be asynchronously loaded • Revealing Module Pattern • we define all of our functions and variables in the private scope and return an anonymous object with pointers to the private functionality we wished to reveal as public. • MVVM
  • 22. AMD PATTERN Application Controller Navigation Data Services Data Context Model • Sequence of loading component might cause an issue.
  • 23. DEFINING A MODULE IN REQUIRE.JS Module ID define('model', Dependencies [‘settings', 'model.workspace'], function (settings, workspace) { var model = { Workspace: workspace }; // ... return model; }); Module Factory
  • 24. STARTING THE SPA Bootstrapper Prime the Data Setup the presentation Data Services HTML Views Data Context View Models Models Navigation Router
  • 25. BOOTSTRAPPER Bootstrapper responsible for initial application run. define('bootstrapper', var run = function () { ['jquery', 'route-config', 'presenter', 'dataprimer', 'binder'], function ($, routeConfig, presenter, dataprimer, binder) { presenter.toggleActivity(true); var run = function () { $.when(dataprimer.fetch()) presenter.toggleActivity(true); $.when(dataprimer.fetch()) .done(binder.bind) .done(binder.bind) .done(routeConfig.register) .done(routeConfig.register) .always(function () { presenter.toggleActivity(false); .always(function () { }); } presenter.toggleActivity(false); return { run : run }} }});
  • 26. DATASERVICE var init = function () { amplify.request.define('activities', 'ajax', { url: '/api/activities', dataType: 'json', type: 'GET' }); }, getActivities = function (callbacks) { return amplify.request({ resourceId: 'activities', data: '', success: callbacks.success, error: callbacks.error });}; init(); return { getActivities: getActivities };
  • 27. VIEWMODEL activityItemTemplate = 'common.activityitem', timelogTemplate = 'common.timelogtable'; activities = ko.observableArray(); activate = function (routerData, callback) { refresh(callback); } getWorkspace = function (callback) { datacontext.activities.getActivities(id); } return { activate: activate activityItemTemplate: activityItemTemplate, timelogTemplate: timelogTemplate }});
  • 28. AS RESULT WE HAVE • Async services • Client side application & business logic • Long-lived client-side state • 2 way data binding • Tombstoning / dropped connection
  • 29. PROS AND CONS • Faster UI • Bad for SEO • More Interactive • More complex to build • Can work offline • Need to expose application logic to client • UI is just another Client • UI can have BI • Perfect for HTML5 and Mobile apps • Require strong JavaScript skills

Hinweis der Redaktion

  1. Today we are going to talk about how to build Single Page Application using ASP.NET MVC4 and JavaScript with bunch of libraries.So, let’s start.
  2. Here is briefly a topics to discuss today. I will focus mostly on client side, because it’s usually much more complex than server side for Single Page Applications.
  3. What is a SPA, (definition from Wikipedia)Sounds like a simple concept, right?What else specific options we can define, which relates to SPA?Application maintain navigation, history of transitions. And application!, but not browser responsible for linking of all pages (or views in terms of SPA).Whole application fits on a single web page. So we don’t need to refresh the page when user click to some internal link.Persisting important state on the client. Server might know nothing what view are opened on clients side, all such information are stored on client.Fully loaded on the initial page load, and progressively downloads data as required.
  4. Typical high level architecture consists of client and server side.On server side we have database, ASP.NET services, which transfer to client all required resources, HTML, CSS, JavaScript)And Data Services which responsible for data access, and asynchronous communication with application.Client side has initialialized HTML page (only one html page per application), CSS files.And JavaScript which might be splitted to several components: Application (component contain business logic)Navigation (maintain transition between views)And Data Access layer which support communication between services, and asynchronous data loading.When client call our application, server returns all required stuff, and call initialization procedure when everything is downloaded.Initialization procedure prepare all components, apply settings. As result, we have complete web application on clientApplication communicate services through Data Access component, and retrieve data on demand.
  5. Server side store the data, so, database on server side is a key component. ORM like EntityFramework will help to access data in DB. Data layer consists of several patterns which provide data in consistent way, and decouple operations and data.Repository pattern which isolate objects from code for accessing them.Unit of work – maintain set of repositories, and manage commit operations, also handles concurrency issues.And our services are exposed in Web API, which is implemented with ASP.NET Web API and using JSON format for transferring data to the client.
  6. ASP.NET providing asynchronous services to client, it works with data layer, receive required entities or collections of entities, and transfer them to client in JSON format.
  7. Typical controller looks like this.Operations call UnitOfWork and define which repository need to use. Call the operation in this repository, and apply some filter or ordering for received data.Than transfer it to client.(I have reduce size of methods, remove exception handling, input values validation, and business logic, just to simplify example, but it’s not ready to production code.)
  8. I’m sure most of you have seen such html code. It usually stands in head files, or at the end of the body.Let me ask you a question, what will do browser? …Yes, it will send to server bunch or requests for each file.And server of course return these files, usually with tabs, spaces, some developers comments.So, you probably get my point, we need to minify these files, and transfer them to client in one response.
  9. What is a SPA, (definition from Wikipedia)Sounds like a simple concept, right?What else specific options we can define, which relates to SPA?Application maintain navigation, history of transitions. And application!, but not browser responsible for linking of all pages (or views in terms of SPA).Whole application fits on a single web page. So we don’t need to refresh the page when user click to some internal link.Persisting important state on the client. Server might know nothing what view are opened on clients side, all such information are stored on client.Fully loaded on the initial page load, and progressively downloads data as required.
  10. What is a SPA, (definition from Wikipedia)Sounds like a simple concept, right?What else specific options we can define, which relates to SPA?Application maintain navigation, history of transitions. And application!, but not browser responsible for linking of all pages (or views in terms of SPA).Whole application fits on a single web page. So we don’t need to refresh the page when user click to some internal link.Persisting important state on the client. Server might know nothing what view are opened on clients side, all such information are stored on client.Fully loaded on the initial page load, and progressively downloads data as required.
  11. What is a SPA, (definition from Wikipedia)Sounds like a simple concept, right?What else specific options we can define, which relates to SPA?Application maintain navigation, history of transitions. And application!, but not browser responsible for linking of all pages (or views in terms of SPA).Whole application fits on a single web page. So we don’t need to refresh the page when user click to some internal link.Persisting important state on the client. Server might know nothing what view are opened on clients side, all such information are stored on client.Fully loaded on the initial page load, and progressively downloads data as required.
  12. What is a SPA, (definition from Wikipedia)Sounds like a simple concept, right?What else specific options we can define, which relates to SPA?Application maintain navigation, history of transitions. And application!, but not browser responsible for linking of all pages (or views in terms of SPA).Whole application fits on a single web page. So we don’t need to refresh the page when user click to some internal link.Persisting important state on the client. Server might know nothing what view are opened on clients side, all such information are stored on client.Fully loaded on the initial page load, and progressively downloads data as required.
  13. What is a SPA, (definition from Wikipedia)Sounds like a simple concept, right?What else specific options we can define, which relates to SPA?Application maintain navigation, history of transitions. And application!, but not browser responsible for linking of all pages (or views in terms of SPA).Whole application fits on a single web page. So we don’t need to refresh the page when user click to some internal link.Persisting important state on the client. Server might know nothing what view are opened on clients side, all such information are stored on client.Fully loaded on the initial page load, and progressively downloads data as required.
  14. What is a SPA, (definition from Wikipedia)Sounds like a simple concept, right?What else specific options we can define, which relates to SPA?Application maintain navigation, history of transitions. And application!, but not browser responsible for linking of all pages (or views in terms of SPA).Whole application fits on a single web page. So we don’t need to refresh the page when user click to some internal link.Persisting important state on the client. Server might know nothing what view are opened on clients side, all such information are stored on client.Fully loaded on the initial page load, and progressively downloads data as required.
  15. What JavaScript libraries might help for this needs:First of all, it’s jQuery for DOM manipulating, retrieving the views (I mean finding them in DOM) and so on.Knockout.js needs for data binding, and implementation of MVVM patternAmplify.js – this library will help to push data to the services and get information from there, it supports caching, so allows to cache some responses.Breeze.js – useful library which allows us create queries to our data, it’s like a linq for JavaScript. By the way… library also supports caching, but I prefer to use caching on layers which is close to server data services.Sammy.js – maintain navigation based on hash tag after url, and store history of user actions. Quite useful right.We will have a lot of JavaScript code, and will be good to organize it well – require.js will help us. It allows to make componetes with javascript classes, and load them asynchronously.Underscore.js – will help to work with collections of elements, and will be interesting for those, who like functional programming.Toastr.js – to create notifications, popups, error messages on UIQunit – we are professionals, and we cannot believe that our code will work well, we need to prove it by unit tests at least, so, I like qunit library for this.That’s even not all libraries, but this is the most significant one. Others will depends on functional and non-functional requirements to our application.
  16. AMD (Asynchronous Module Definition)is a JavaScript API for defining modules and their dependencies in such a way that modules may be asynchronously loadedRevealing Module Patternwe define all of our functions and variables in the private scope and return an anonymous object with pointers to the private functionality we wished to reveal as public.MVVMMVVM facilitates a clear separation of the development of the graphical user interface from the development of the business logic or back end logic known as the model 
  17. What is a SPA, (definition from Wikipedia)Sounds like a simple concept, right?What else specific options we can define, which relates to SPA?Application maintain navigation, history of transitions. And application!, but not browser responsible for linking of all pages (or views in terms of SPA).Whole application fits on a single web page. So we don’t need to refresh the page when user click to some internal link.Persisting important state on the client. Server might know nothing what view are opened on clients side, all such information are stored on client.Fully loaded on the initial page load, and progressively downloads data as required.
  18. What is a SPA, (definition from Wikipedia)Sounds like a simple concept, right?What else specific options we can define, which relates to SPA?Application maintain navigation, history of transitions. And application!, but not browser responsible for linking of all pages (or views in terms of SPA).Whole application fits on a single web page. So we don’t need to refresh the page when user click to some internal link.Persisting important state on the client. Server might know nothing what view are opened on clients side, all such information are stored on client.Fully loaded on the initial page load, and progressively downloads data as required.
  19. What is a SPA, (definition from Wikipedia)Sounds like a simple concept, right?What else specific options we can define, which relates to SPA?Application maintain navigation, history of transitions. And application!, but not browser responsible for linking of all pages (or views in terms of SPA).Whole application fits on a single web page. So we don’t need to refresh the page when user click to some internal link.Persisting important state on the client. Server might know nothing what view are opened on clients side, all such information are stored on client.Fully loaded on the initial page load, and progressively downloads data as required.
  20. What is a SPA, (definition from Wikipedia)Sounds like a simple concept, right?What else specific options we can define, which relates to SPA?Application maintain navigation, history of transitions. And application!, but not browser responsible for linking of all pages (or views in terms of SPA).Whole application fits on a single web page. So we don’t need to refresh the page when user click to some internal link.Persisting important state on the client. Server might know nothing what view are opened on clients side, all such information are stored on client.Fully loaded on the initial page load, and progressively downloads data as required.
  21. Here is my contacts, please feel free to contact me if you will have any questions according to SPA, or subject that we discussed.I will be glad to discuss different approaches in building SPA.Do you have any questions?