Download - Adventure Time with JavaScript & Single Page Applications

Transcript
Page 1: Adventure Time with JavaScript & Single Page Applications

Adventure Timewith

JavaScript & Single Page Apps - Web Unleashed 2013 - Carl Bergenhem @carlbergenhem

Page 2: Adventure Time with JavaScript & Single Page Applications

IntroductionsCarl Bergenhem

Manager, Solutions Consultants

Telerik

@carlbergenhem

Page 3: Adventure Time with JavaScript & Single Page Applications

Quick Notes

Page 4: Adventure Time with JavaScript & Single Page Applications

Covering a lot of ground today

Page 5: Adventure Time with JavaScript & Single Page Applications

High level overview

Page 6: Adventure Time with JavaScript & Single Page Applications

More code in tomorrow'sworkshop

9 AM - 12:30 PM @ Room 325 BC

Page 7: Adventure Time with JavaScript & Single Page Applications

What is a SPA?

Page 8: Adventure Time with JavaScript & Single Page Applications

Single Page Application

Page 9: Adventure Time with JavaScript & Single Page Applications

A web app that fits in a singlepage

Page 10: Adventure Time with JavaScript & Single Page Applications

Entire app is loaded at once

Page 11: Adventure Time with JavaScript & Single Page Applications

Built with a MV* Framework

Page 12: Adventure Time with JavaScript & Single Page Applications

Server provides the initial app

Page 13: Adventure Time with JavaScript & Single Page Applications

Section our HTML page in views

Page 14: Adventure Time with JavaScript & Single Page Applications

Get data via RESTful requests

Page 15: Adventure Time with JavaScript & Single Page Applications

Bind the data to our UI

Page 16: Adventure Time with JavaScript & Single Page Applications

Business logic in our MV*framework

Page 17: Adventure Time with JavaScript & Single Page Applications

MV* framework handles URLrequests

Page 18: Adventure Time with JavaScript & Single Page Applications

Why SPA?

Page 19: Adventure Time with JavaScript & Single Page Applications

Create desktop-like apps in theweb

Page 20: Adventure Time with JavaScript & Single Page Applications

Increase responsiveness

Page 21: Adventure Time with JavaScript & Single Page Applications

Reduce loadtime of pages

Page 22: Adventure Time with JavaScript & Single Page Applications

Less time spent on the server

Page 23: Adventure Time with JavaScript & Single Page Applications

Control user interactions on theclient

Page 24: Adventure Time with JavaScript & Single Page Applications

Improved end-user experience

Page 25: Adventure Time with JavaScript & Single Page Applications

History

Page 26: Adventure Time with JavaScript & Single Page Applications

In the BeginningReign of the post back

Page 27: Adventure Time with JavaScript & Single Page Applications

Everything done on the server

Page 28: Adventure Time with JavaScript & Single Page Applications

Full page refreshes

Page 29: Adventure Time with JavaScript & Single Page Applications

We got wiserAjax & XHR Requests

Page 30: Adventure Time with JavaScript & Single Page Applications

Serve some content via server

Page 31: Adventure Time with JavaScript & Single Page Applications

Do asynchronous calls to server

Page 32: Adventure Time with JavaScript & Single Page Applications

Partial page refreshes

Page 33: Adventure Time with JavaScript & Single Page Applications

Focus onresponsivenessIntroduce the SPA concept

Page 34: Adventure Time with JavaScript & Single Page Applications

Move all items over to the client

Page 35: Adventure Time with JavaScript & Single Page Applications

What makes up aSPA?

Page 36: Adventure Time with JavaScript & Single Page Applications

MV* Framework

Page 37: Adventure Time with JavaScript & Single Page Applications

Basic pieces of a MV*ModelView*

Page 38: Adventure Time with JavaScript & Single Page Applications

A full SPA framework also hasRouting

Page 39: Adventure Time with JavaScript & Single Page Applications

Model

Page 40: Adventure Time with JavaScript & Single Page Applications

In charge of holding data

Page 41: Adventure Time with JavaScript & Single Page Applications

Communication with server

Page 42: Adventure Time with JavaScript & Single Page Applications

Work with server end-points

Page 43: Adventure Time with JavaScript & Single Page Applications

Define what data should beused

Page 44: Adventure Time with JavaScript & Single Page Applications

Controller

Page 45: Adventure Time with JavaScript & Single Page Applications

Control applications'StateLogicBehavior

Page 46: Adventure Time with JavaScript & Single Page Applications

Custom business logic

Page 47: Adventure Time with JavaScript & Single Page Applications

Communicate between theView and Model

Page 48: Adventure Time with JavaScript & Single Page Applications

Provide validation

Page 49: Adventure Time with JavaScript & Single Page Applications

Bind data to our Views (UI)

Page 50: Adventure Time with JavaScript & Single Page Applications

View

Page 51: Adventure Time with JavaScript & Single Page Applications

Responsible for the UI of ourapp

Page 52: Adventure Time with JavaScript & Single Page Applications

Modifies and interacts with ourDOM

Page 53: Adventure Time with JavaScript & Single Page Applications

Main point of interaction fromusers

Page 54: Adventure Time with JavaScript & Single Page Applications

Bound to the controller for dataand events

Page 55: Adventure Time with JavaScript & Single Page Applications

Often works with a templateengine

Page 56: Adventure Time with JavaScript & Single Page Applications

Router

Page 57: Adventure Time with JavaScript & Single Page Applications

Responds to URL changes viaJavaScript

Page 58: Adventure Time with JavaScript & Single Page Applications

Changes between variousviews

Page 59: Adventure Time with JavaScript & Single Page Applications

Browser initiated routesType in a URLHyperlink

Page 60: Adventure Time with JavaScript & Single Page Applications

Client initiated routes

Page 61: Adventure Time with JavaScript & Single Page Applications

Change from one page or aview to another

Page 62: Adventure Time with JavaScript & Single Page Applications

Utilizes hash fragments

http://myurl.com/#/products/

Page 63: Adventure Time with JavaScript & Single Page Applications

JS rights to modify page URLAnything after '#' is fair game

Page 64: Adventure Time with JavaScript & Single Page Applications

Just like the anchor tag

Page 65: Adventure Time with JavaScript & Single Page Applications

Examples

Page 66: Adventure Time with JavaScript & Single Page Applications

Wild SPA AppsGmailFacebokTwitterTrelloAsana

Page 67: Adventure Time with JavaScript & Single Page Applications

Popular MV*Frameworks

Page 68: Adventure Time with JavaScript & Single Page Applications

BackboneAngularKendo UIEmberDurandalMeteor

Page 69: Adventure Time with JavaScript & Single Page Applications

Today's FocusBackboneAngularKendo UI

Page 70: Adventure Time with JavaScript & Single Page Applications

Backbone

Page 71: Adventure Time with JavaScript & Single Page Applications

Light-weight library

Page 72: Adventure Time with JavaScript & Single Page Applications

The basic foundation

Page 73: Adventure Time with JavaScript & Single Page Applications

Not a ton of extra frills

Page 74: Adventure Time with JavaScript & Single Page Applications

Heavy lifting is up to you

Page 75: Adventure Time with JavaScript & Single Page Applications

Huge community for tooling

Page 76: Adventure Time with JavaScript & Single Page Applications

Offers a set of helpful classes

Page 77: Adventure Time with JavaScript & Single Page Applications

Each one can- and will be -extended

Page 78: Adventure Time with JavaScript & Single Page Applications

Backbone main classesModelCollectionViewRouter

Page 79: Adventure Time with JavaScript & Single Page Applications

Model

Page 80: Adventure Time with JavaScript & Single Page Applications

Deals with all of our data

Page 81: Adventure Time with JavaScript & Single Page Applications

Bound to a view

Page 82: Adventure Time with JavaScript & Single Page Applications

Syncs back to our server

Page 83: Adventure Time with JavaScript & Single Page Applications

Backbone.Modelvar myModel = new Backbone.Model({});

Page 84: Adventure Time with JavaScript & Single Page Applications

Extend Methodvar Character = Backbone.Model.extend({ defaults: { name: 'Blanks', species: 'Human', description: 'No description yet.' }, initialize: function() { console.log('New character created'); }});

var jake = new Character({ name: 'Jake the Dog', species: 'Dog', description: 'A magical dog. Best friend, and brother, with Finn.'});

Page 85: Adventure Time with JavaScript & Single Page Applications

Use get() or set() to changedata

var finn = new Character({ name: 'Finn the Human' });

finn.set({ species: 'Human' });

var name = finn.get('name'); //Finn the Human

Page 86: Adventure Time with JavaScript & Single Page Applications

Collections

Page 87: Adventure Time with JavaScript & Single Page Applications

Group related models together

Page 88: Adventure Time with JavaScript & Single Page Applications

Methods for adding/removingmodels

Page 89: Adventure Time with JavaScript & Single Page Applications

Simple array-like object

Page 90: Adventure Time with JavaScript & Single Page Applications

Backbone.Collectionvar Character = Backbone.Model.extend({ defaults: { name: 'Blanks', species: 'Human', description: 'No description yet.' }, initialize: function() { console.log('New character created'); }});

var Cast = Backbone.Collection.extend({ model: Character});

var jake = new Character({ name: 'Jake the Dog', species: 'Dog', description: 'A magical dog. Best friend, and brother, with Finn.'});

var finn = new Character({ name: 'Finn the Human', species: 'Human', description: 'The last human in the Land of Ooo. Hangs out with Jake.'});

var castList = new Cast([ jake, finn ]);

console.log(castList.models);

Page 91: Adventure Time with JavaScript & Single Page Applications

View

Page 92: Adventure Time with JavaScript & Single Page Applications

The link between models andthe UI

Page 93: Adventure Time with JavaScript & Single Page Applications

Renders the models and theirdata

Page 94: Adventure Time with JavaScript & Single Page Applications

Views are bound to models orcollections

Page 95: Adventure Time with JavaScript & Single Page Applications

Receives events from theModel and HTML document

Page 96: Adventure Time with JavaScript & Single Page Applications

The 'el' propertyEvery view has an 'el' propertyIf not defined, a blank div is createdReferences the view's DOM objectPass in a HTML element to modify itCan be a jQuery object via $el

Page 97: Adventure Time with JavaScript & Single Page Applications

Backbone.Viewvar FinnView = Backbone.View.extend({ tagName: 'div', id: 'finnDiv', className: 'finnClass', attributes: { 'data-custom': 12345 }});

var quickView = new FinnView();

$('#placeholder').prepend(quickView.el);

Page 98: Adventure Time with JavaScript & Single Page Applications

Resulting HTML<div data-custom="12345" id="sampleDiv" class="sampleClass"></div>

Page 99: Adventure Time with JavaScript & Single Page Applications

Default templates byUnderscorejsBin.com/IWewESE/8/

Page 100: Adventure Time with JavaScript & Single Page Applications

Router

Page 101: Adventure Time with JavaScript & Single Page Applications

Use of hash fragments

http://myurl.com/#/characters/

Page 102: Adventure Time with JavaScript & Single Page Applications

Parses URL and findsappropriate function

Page 103: Adventure Time with JavaScript & Single Page Applications

Uses HTML5 History API ifpossible

Page 104: Adventure Time with JavaScript & Single Page Applications

Backbone.Routervar SampleRouter = Backbone.Router.extend({ routes: { 'character/:name': 'character' //route pattern : handler name }, character: function(name) { //handler //... }});

Page 105: Adventure Time with JavaScript & Single Page Applications

BackboneSummary

Page 106: Adventure Time with JavaScript & Single Page Applications

Barebones tools to build ourapp

Page 107: Adventure Time with JavaScript & Single Page Applications

Emphasis on data and Models

Page 108: Adventure Time with JavaScript & Single Page Applications

Not a ton of tools for the View

Page 109: Adventure Time with JavaScript & Single Page Applications

.extend({}) everything

Page 110: Adventure Time with JavaScript & Single Page Applications

Backbone main classesModelCollectionViewRouter

Page 111: Adventure Time with JavaScript & Single Page Applications

Model deals with all of our data

Page 112: Adventure Time with JavaScript & Single Page Applications

A collection has multipleModels

Page 113: Adventure Time with JavaScript & Single Page Applications

View needs a ModelWorks with Model and DOM

Page 114: Adventure Time with JavaScript & Single Page Applications

Router works with URL to servecorrect View

Page 115: Adventure Time with JavaScript & Single Page Applications

Angular

Page 116: Adventure Time with JavaScript & Single Page Applications

Created by Google

Page 117: Adventure Time with JavaScript & Single Page Applications

Believes HTML can be morepowerful

Page 118: Adventure Time with JavaScript & Single Page Applications

Extends HTML vocabulary

Page 119: Adventure Time with JavaScript & Single Page Applications

Tries to follow a pure HTMLpath

Page 120: Adventure Time with JavaScript & Single Page Applications

Angular componentsExpressionsControllersDirectivesRouting

Page 121: Adventure Time with JavaScript & Single Page Applications

Expressions

Page 122: Adventure Time with JavaScript & Single Page Applications

Allows us to bring data to theview

Page 123: Adventure Time with JavaScript & Single Page Applications

Uses a simple syntax{{ model.FieldName }}

Page 124: Adventure Time with JavaScript & Single Page Applications

One-way binding<div ng-controller="SampleController"> <span>{{ character.name }}</span></div>

Page 125: Adventure Time with JavaScript & Single Page Applications

Two-way binding<select ng-model="selectedCharacter"> <option value="0">Finn</option> <option value="1">Jake</option> <option value="2">BMO</option></select>

Page 126: Adventure Time with JavaScript & Single Page Applications

Controllers

Page 127: Adventure Time with JavaScript & Single Page Applications

Central component of Angularapps

Page 128: Adventure Time with JavaScript & Single Page Applications

Contains data, logic, and states

Page 129: Adventure Time with JavaScript & Single Page Applications

Creates a scope for the HTMLelement

$scope variable

Page 130: Adventure Time with JavaScript & Single Page Applications

Sample Controllervar sampleAppControllers = angular.module('sampleAppControllers', []);

sampleAppControllers.controller('ShowController', ['$scope', function ShowController($scope) { $scope.sampleVariable = 1; $scope.sampleArray = [{ fieldOne: 0, fieldTwo: "Test" }, { fieldOne: 1, fieldTwo: "Sample" }] $scope.sampleFunction: function() { //Function logic } }]);

Page 131: Adventure Time with JavaScript & Single Page Applications

$scope only available within theHTML element it is defined in

Page 132: Adventure Time with JavaScript & Single Page Applications

Directives

Page 133: Adventure Time with JavaScript & Single Page Applications

HTML-like attributes

Page 134: Adventure Time with JavaScript & Single Page Applications

Declared in view code

Page 135: Adventure Time with JavaScript & Single Page Applications

Helps with setting upAngular AppControllers and bindingAttributesConditional displayEvents

Page 136: Adventure Time with JavaScript & Single Page Applications

Sample Directivesng-app="appName"ng-controller="SampleCtrl"ng-model="ScopeVar"ng-src="{{scopeUrlVar}}"ng-click="eventHandler()"

Page 137: Adventure Time with JavaScript & Single Page Applications

Routing

Page 138: Adventure Time with JavaScript & Single Page Applications

Works with URL like you'd think

Page 139: Adventure Time with JavaScript & Single Page Applications

Single main viewPlaceholder element for content

Page 140: Adventure Time with JavaScript & Single Page Applications

Load other views via partialsJust HTML pages

Page 141: Adventure Time with JavaScript & Single Page Applications

Simple structure for routingvar sampleApp = angular.module('sampleApp', [ 'ngRoute', 'myControllers']);;

sampleApp.config(['$routeProvider', function($routeProvider) { $routeProvider.when('/test', { templateUrl: 'partials/test.html', controller: $routeProvider.when('/sample:id', { templateUrl: 'partials/sample.html', $routeProvider.otherwise({ redirectTo: '/' }); }]);

Page 142: Adventure Time with JavaScript & Single Page Applications

AngularSummary

Page 143: Adventure Time with JavaScript & Single Page Applications

Extension of HTML

Page 144: Adventure Time with JavaScript & Single Page Applications

Empowering your regular HTMLcode

Page 145: Adventure Time with JavaScript & Single Page Applications

Controllers rule all

Page 146: Adventure Time with JavaScript & Single Page Applications

Data can be bound one- or two-way

Page 147: Adventure Time with JavaScript & Single Page Applications

DOM manipulation viacontroller

Drop that jQuery ;)

Page 148: Adventure Time with JavaScript & Single Page Applications

Directives are extremely helpfulWire upp events and such

Page 149: Adventure Time with JavaScript & Single Page Applications

Routers use partials of HTML

Page 150: Adventure Time with JavaScript & Single Page Applications

Kendo UI

Page 151: Adventure Time with JavaScript & Single Page Applications

Part of Kendo UI Web

Page 152: Adventure Time with JavaScript & Single Page Applications

Combine SPA and MVVMFrameworks

Page 153: Adventure Time with JavaScript & Single Page Applications

Additional helpful itemsTemplate engineEffects and animationsUI widgetsCharts and gauges

Page 154: Adventure Time with JavaScript & Single Page Applications

Tooling across all parts of yourSPA

Page 155: Adventure Time with JavaScript & Single Page Applications

Kendo UI key piecesObservableViewLayoutRouter

Page 156: Adventure Time with JavaScript & Single Page Applications

Observable

Page 157: Adventure Time with JavaScript & Single Page Applications

Part of the Kendo UI MVVMframework

Page 158: Adventure Time with JavaScript & Single Page Applications

All fields are observable

Page 159: Adventure Time with JavaScript & Single Page Applications

All changes will propogate

Page 160: Adventure Time with JavaScript & Single Page Applications

kendo.observablevar finnModel = kendo.observable({ name: 'Finn the Human', species: 'Human', description: 'The last human in the Land of Ooo. Hangs out with Jake.'});

Page 161: Adventure Time with JavaScript & Single Page Applications

Use get() or set() with fieldsvar finnModel = kendo.observable({ name: 'Finn the Human', species: 'Dog', description: 'The last human in the Land of Ooo. Hangs out with Jake.'});

finnModel.set('species', 'Human');

var species = finnModel.get('species'); //Human

Page 162: Adventure Time with JavaScript & Single Page Applications

View

Page 163: Adventure Time with JavaScript & Single Page Applications

Can be defined with simpleHTML string

Page 164: Adventure Time with JavaScript & Single Page Applications

Works with Kendo UI templates

Page 165: Adventure Time with JavaScript & Single Page Applications

Tie in with our observables tocreate data bound UI

Page 166: Adventure Time with JavaScript & Single Page Applications

kendo.View()var index = new kendo.View('<span>Adventure Time!</span>');

index.render('#placeholder');

Page 167: Adventure Time with JavaScript & Single Page Applications

With Kendo UI Templates andObservables

jsbin.com/imAnimo/5/

Page 168: Adventure Time with JavaScript & Single Page Applications

Layout

Page 169: Adventure Time with JavaScript & Single Page Applications

Provides a layout for views toadhere to

Page 170: Adventure Time with JavaScript & Single Page Applications

MasterPages for you .NET folks

Page 171: Adventure Time with JavaScript & Single Page Applications

Easily display views in contentsection

Page 172: Adventure Time with JavaScript & Single Page Applications

Can also be nested

Page 173: Adventure Time with JavaScript & Single Page Applications

kendo.Layoutvar view = new Kendo.View('<span>Finn the Human</span>');

var layout = new kendo.Layout('<header>Header</header><div id="content"></div><footer>Footer</footer>');

layout.render($('#app'));

layout.showIn('#content', view);

Page 174: Adventure Time with JavaScript & Single Page Applications

Router

Page 175: Adventure Time with JavaScript & Single Page Applications

Responsible for applicationstate

Page 176: Adventure Time with JavaScript & Single Page Applications

Navigate between views

Page 177: Adventure Time with JavaScript & Single Page Applications

Use of hash fragments

http://myurl.com/#/characters/

Page 178: Adventure Time with JavaScript & Single Page Applications

Can programmatically navigate

Page 179: Adventure Time with JavaScript & Single Page Applications

Use with Layout to show newcontent

Page 180: Adventure Time with JavaScript & Single Page Applications

kendo.Router()var router = new kendo.Router();router.route('/', function () { layout.showIn('#content', homeView);});

router.route('/character', function () { layout.showIn('#content', characterView);});

$(function() { router.start(); //very important

router.navigate('/character');});

Page 181: Adventure Time with JavaScript & Single Page Applications

Kendo UISummary

Page 182: Adventure Time with JavaScript & Single Page Applications

Tools across the board

Page 183: Adventure Time with JavaScript & Single Page Applications

Classes for dealing with data

Page 184: Adventure Time with JavaScript & Single Page Applications

Helpful UI and View classes

Page 185: Adventure Time with JavaScript & Single Page Applications

Large set of UI widgets

Page 186: Adventure Time with JavaScript & Single Page Applications

Kendo UI SPA key itemsObservableViewLayoutRouter

Page 187: Adventure Time with JavaScript & Single Page Applications

The EndThanks for attending!

@carlbergenhem

github.com/bergenhem/talks