TypeScript - a look at SPA's and Angular 2 Christian Holm Diget - - PowerPoint PPT Presentation

typescript
SMART_READER_LITE
LIVE PREVIEW

TypeScript - a look at SPA's and Angular 2 Christian Holm Diget - - PowerPoint PPT Presentation

TypeScript - a look at SPA's and Angular 2 Christian Holm Diget Agenda EcmaScript 6 TypeScript Libraries: ImmutableJS + React SPA architecture Angular 2.0 EcmaScript 6/2015 Let + const function f() { { let x; { //


slide-1
SLIDE 1

TypeScript ❤

  • a look at SPA's and Angular 2

Christian Holm Diget

slide-2
SLIDE 2

Agenda

  • EcmaScript 6
  • TypeScript
  • Libraries: ImmutableJS + React
  • SPA architecture
  • Angular 2.0
slide-3
SLIDE 3
slide-4
SLIDE 4

EcmaScript 6/2015

slide-5
SLIDE 5

Let + const

function f() { { let x; { // okay, block scoped name const x = "valid"; // error, const x = "bar"; } // error, already declared in block let x = "inner"; } }

slide-6
SLIDE 6

Arrow functions

var self = this; $('body').on('click', 'a', function(e) { //self }); $('body').on('click', 'a', e => { //this });

slide-7
SLIDE 7

for…of

for (var n of fibonacci) { // stop at 1000 if (n > 1000) break; console.log(n); }

slide-8
SLIDE 8

Template strings

var car = "Porche", feeling = "awesome"; `My ${car} is making me feel ${feeling}!`

slide-9
SLIDE 9

Default arguments

function f(x, y = 100) { return x + y; }

slide-10
SLIDE 10

Rest

function f(x, ...y) { return x * y.length; } f(100, "volvo", true);

slide-11
SLIDE 11

Spread

function addThree(x, y, z) { return x + y + z; } addThree(...[100, 50, 25]);

slide-12
SLIDE 12

Destructuring

var [a, , b] = [6, 12, 18]; var {name, cylinders, color} = {name: "Audi", cylinders: 8, color: "black"};

slide-13
SLIDE 13

Modules

export function fiat() { return "Fiat" } export function peugeot() { return "Peugeot" } import * as Cars from "my/cars"; import { fiat, peugeot } from "my/cars";

slide-14
SLIDE 14

Classes

class Ferrari extends Car { constructor(cylinders, color) { super(cylinders, color); this.color = color; } start() { super.start(); } static race(cars) { cars.forEach(car => car.start() ); } }

slide-15
SLIDE 15
slide-16
SLIDE 16

TypeScript

  • Superset af JavaScript
  • Brug ES6 features og compile til ES3 eller ES5
  • Static type checking
  • Interfaces
  • Generics
  • Tooling
slide-17
SLIDE 17

Types

  • Null, string, number, boolean, undefined
  • Object, array
  • Interface, Class, Enum, Any

function ToString(name: string, age: number) : string { return name + age; };

slide-18
SLIDE 18

Default and optional

function ToString(firstName : string = "John Doe", lastName?: string): string { if (lastName) { return firstName + " " + lastName; } return firstName; };

slide-19
SLIDE 19

Classes

class Person { public age: number; constructor(public name: string, ageParam: number) { this.age = ageParam; } public ToString() : string { return this.name + this.age; } }

slide-20
SLIDE 20

Generics

class Collection<T> { private items: Array<T>; public Add(item: T) { this.items.push(item); } }

slide-21
SLIDE 21

Interfaces

interface IPrintable { ToString() : string; } class Person implements IPrintable { public age: number; constructor(public name: string, ageParam: number) { this.age = ageParam; } public ToString() : string { return this.name + this.age; } }

slide-22
SLIDE 22

Definitions

  • https://github.com/borisyankov/DefinitelyTyped

declare function domready(callback: () => any): void; declare module "domready" { export = domready; }

slide-23
SLIDE 23

Adoption

slide-24
SLIDE 24

ImmutableJS

var map1: Immutable.Map<string, number>; map1 = Immutable.Map({ foo: 1, bar: 2, baz: 3 }); var map2 = map1.set('bar', 25); map1.get('bar'); // 2 map2.get('bar'); // 25

Data structures List, Map, Set, Records etc. Sequences Convertion from/to objects

slide-25
SLIDE 25

Sequences

Immutable.Range(1, Infinity) .skip(1000) .map(n => -n) .filter(n => n % 2 === 0) .take(2) .reduce((r, n) => r * n, 1);

slide-26
SLIDE 26

React

TS 1.6 comes with JSX support (tsx) var HelloWorld = React.createClass({ render: function () { return <div>Hello {this.props.name } </div>; } }); React.render(<HelloWorld name="John" />, mountNode);

slide-27
SLIDE 27

Single Page Application Architektur Services Components Komposition / DI Data Views Binding

Interaction og rendering System arkitektur

slide-28
SLIDE 28

Libraries vs frameworks

slide-29
SLIDE 29
  • 1. Build small components
  • 2. Composition over inheritence
  • 3. Loose coupling, high cohesion
slide-30
SLIDE 30

Application architecture

slide-31
SLIDE 31

Services

export class CarService { public doStuff(car : Car) { ... } }

slide-32
SLIDE 32

Composition with SystemJS

/* Configure */ System.config({ baseURL: '/lib/', paths: { '*': '*.js' } }); System.import('app'); /* Use in App.js */ var carFactory = require('./cars').carFactory; var audi = carFactory("Audi", 8);

slide-33
SLIDE 33
slide-34
SLIDE 34

Components

class ProductWidget { constructor(private el: HTMLElement, private product: Product) { } public Render() { var output = Mustache.render( 'Navn: {{product.name}} ' + 'Beskrivelse: {{product.description}}', product); this.el.innerHTML = output; } public SetPrice(price: number) { ... } }; new ProductWidget(document.getElementById("prod1"), new Product("Red Bull", "Gi’r vinger", 20));

slide-35
SLIDE 35

View rendering

slide-36
SLIDE 36

Templates with Mustache

var model = { cars: [ { make: "Audi", model: "A1" }, { make: "Chevrolet", model: "Aveo" } ] }; var output = Mustache.render( '{{#cars}}<tr><td>{{make}}</td><td>{{model}}</td></tr>{{/cars}}', model); document.getElementById("demo").innerHTML = output;

slide-37
SLIDE 37

Data… Routing, validation etc.

Q.xhr.get('/api/products').done((response) => { ... });

slide-38
SLIDE 38
slide-39
SLIDE 39

Angular 2.0 alpha

  • Mobile first
  • 5x faster according to google
  • Fewer concepts
  • Based on webstandards
  • AtScript -> TypeScript
  • angular.io
  • angular-tips.com
slide-40
SLIDE 40

Angular 2 languages

  • ES5, ES6
  • TypeScript
  • Dart
slide-41
SLIDE 41

Dependencies

  • traceur-runtime.js (transpiler)
  • system.js (dynamic module loader)
  • angular2.d.ts
  • rx.d.ts
  • es6-promise.d.ts
  • https://code.angularjs.org/2.0.0-alpha.37/angular2.dev.js
slide-42
SLIDE 42

Annotations

@Component({ selector: 'mywidget‘ }) @View({ template: '<div *for="#element in elements">{{element.someProperty}}</div>', directives: [For] })

slide-43
SLIDE 43

View Syntax

  • Interpolation: {{ expression | pipe }}
  • Variable: #variable
  • Event bindings: (click) = ""
  • Property bindings: [hidden] = ""
  • Template directive: *ng-for, *ng-if
slide-44
SLIDE 44

Components

  • Application = tree of components

@Component({ selector: 'mywidget' }) @View({ template: '<div *ng-for="#element in elements">{{element.someProperty}}</div>', directives: [NgFor] }) class MyWidget { constructor(myService: MyService) { … } }

slide-45
SLIDE 45

Dependency Injection

import {CarService} from 'carService'; @Component({ selector: 'carwidget' }) @View({…}) class CarWidget { constructor(carService: CarService) {…} }

slide-46
SLIDE 46

Takeaways

  • EcmaScript 6
  • …and how to use it today
  • TypeScript
  • ImmutableJS
  • SPA architecture and patterns
  • A glimpse into Angular 2.0

christian@dotnetnerd.dk www.dotnetnerd.dk @dotnetnerd

slide-47
SLIDE 47
slide-48
SLIDE 48