TypeScript ❤
- a look at SPA's and Angular 2
Christian Holm Diget
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; { //
Christian Holm Diget
function f() { { let x; { // okay, block scoped name const x = "valid"; // error, const x = "bar"; } // error, already declared in block let x = "inner"; } }
var self = this; $('body').on('click', 'a', function(e) { //self }); $('body').on('click', 'a', e => { //this });
for (var n of fibonacci) { // stop at 1000 if (n > 1000) break; console.log(n); }
var car = "Porche", feeling = "awesome"; `My ${car} is making me feel ${feeling}!`
function f(x, y = 100) { return x + y; }
function f(x, ...y) { return x * y.length; } f(100, "volvo", true);
function addThree(x, y, z) { return x + y + z; } addThree(...[100, 50, 25]);
var [a, , b] = [6, 12, 18]; var {name, cylinders, color} = {name: "Audi", cylinders: 8, color: "black"};
export function fiat() { return "Fiat" } export function peugeot() { return "Peugeot" } import * as Cars from "my/cars"; import { fiat, peugeot } from "my/cars";
class Ferrari extends Car { constructor(cylinders, color) { super(cylinders, color); this.color = color; } start() { super.start(); } static race(cars) { cars.forEach(car => car.start() ); } }
function ToString(name: string, age: number) : string { return name + age; };
function ToString(firstName : string = "John Doe", lastName?: string): string { if (lastName) { return firstName + " " + lastName; } return firstName; };
class Person { public age: number; constructor(public name: string, ageParam: number) { this.age = ageParam; } public ToString() : string { return this.name + this.age; } }
class Collection<T> { private items: Array<T>; public Add(item: T) { this.items.push(item); } }
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; } }
declare function domready(callback: () => any): void; declare module "domready" { export = domready; }
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
Immutable.Range(1, Infinity) .skip(1000) .map(n => -n) .filter(n => n % 2 === 0) .take(2) .reduce((r, n) => r * n, 1);
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);
Interaction og rendering System arkitektur
export class CarService { public doStuff(car : Car) { ... } }
/* Configure */ System.config({ baseURL: '/lib/', paths: { '*': '*.js' } }); System.import('app'); /* Use in App.js */ var carFactory = require('./cars').carFactory; var audi = carFactory("Audi", 8);
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));
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;
Q.xhr.get('/api/products').done((response) => { ... });
@Component({ selector: 'mywidget‘ }) @View({ template: '<div *for="#element in elements">{{element.someProperty}}</div>', directives: [For] })
@Component({ selector: 'mywidget' }) @View({ template: '<div *ng-for="#element in elements">{{element.someProperty}}</div>', directives: [NgFor] }) class MyWidget { constructor(myService: MyService) { … } }
import {CarService} from 'carService'; @Component({ selector: 'carwidget' }) @View({…}) class CarWidget { constructor(carService: CarService) {…} }
christian@dotnetnerd.dk www.dotnetnerd.dk @dotnetnerd