Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Angular2 et les standards du Web

Angular2 et les standards du Web

Conférence donnée au DevFest Nantes 2016

Emmanuel DEMEY

November 10, 2016
Tweet

More Decks by Emmanuel DEMEY

Other Decks in Programming

Transcript

  1. Internationalisation API Décorateurs ECMAScript 2015 Observables Zones HTML Templates Shadow

    DOM Custom Elements DOM Metadata Reflection API Fetch WebWorkers PWA
  2. TC39 Stage 0 - Strawman Stage 1 - Proposal Stage

    2 - Draft Stage 3 - Candidate Stage 4 - Finished
  3. Oui mais… pourquoi dans Angular2 new Intl.Collator(locale, options) String.prototype.localeCompare(locale, options)

    new Intl.DateTimeFormat(locale, options) Date.prototype.toLocaleString(locale, options) Date.prototype.toLocaleDateString(locale, options) Date.prototype.toLocaleTimeString(locale, options) new Intl.NumberFormat(locale, options) Number.prototype.toLocaleString(locale, options)
  4. DateTimeFormat /* Code */ let formatter = new Intl.DateTimeFormat( “en”,{});

    formatter.format(new Date(Date.UTC(2016,10,10,3,0,0))) “11/10/2016” “10/11/2016” let formatter = new Intl.DateTimeFormat( “fr”,{ }); formatter.format(new Date(Date.UTC(2016,10,10,3,0,0))); month: “long” minute: “numeric”, /* 2-digit */ day: “numeric”, /* 2-digit */ year: “numeric”, /* 2-digit */ “novembre” hour: “numeric”, /* 2-digit */ “11 novembre 2016” “11 novembre 2016 04h” “11 novembre 2016 04:00”
  5. NumberFormat /* Code */ let formatter = new Intl.NumberFormat( “en”,{});

    formatter.format(1000.59) “1,000.59” “1 000,59” let formatter = new Intl.NumberFormat( “fr”,{ }); formatter.format(1000.57); style: “currency”, /* percent, number */ maximumFractionDigits: 1 currency: “EUR”, /* USD */ currencyDisplay: “name”, /* symbol */ useGrouping: false, “1 000,59 euros” “1000,59 euros” “1000,6 euros”
  6. Oui mais… pourquoi dans Angular2 @Pipe({name:”number”}) class NumberPipe { }

    @Pipe({name:”percent”}) class PercentPipe { } @Pipe({name:”currency”}) class CurrencyPipe { } @Pipe({name:”date”}) class DatePipe { }
  7. Oui mais… pourquoi dans Angular2 • Internationalisation du contenu ◦

    Utilisation de la directive i18n ◦ Extraction des tous les messages ◦ Traduction (via un fichier .xlf) ◦ Intégration JIT ou AOT ▪ SystemJS, WebPack ou ngc
  8. Oui mais… pourquoi dans Angular2 <p i18n=”Welcome Message”> Hello Nantes!

    </p> ./node_modules/.bin/ng-xi18n <trans-unit> <source>Hello Nantes! </source> <target></target> <note from=”description”>Welcome Message</note> </trans-unit>
  9. Les Décorateurs • Fonctions pour ajouter des métadonnées à des

    objets JS ◦ Property, Method, Class, Parameter
  10. Les différentes signatures declare type ClassDecorator = (target: Function) =>

    Function declare type PropertyDecorator = (target: Object, propertyKey: string) => void; declare type MethodDecorator = (target: Object, propertyKey: string, descriptor: TypedPropertyDescriptor) => TypedPropertyDescriptor; declare type ParameterDecorator = (target: Function, propertyKey: string, parameterIndex: number) => void;
  11. Mon premier décorateur... @log sayHello(){ … } function log(target,key,descriptor){ return

    descriptor; } let orig = descriptor.value; descriptor.value = function(...args){ console.log(“function called”); return orig.apply(this, args); }
  12. Mon deuxième décorateur... function Injectable(target){ let original = target; let

    newC = function(...args){ args = args.map(s => injector.get(s)); return original.apply(this, args); } newC.prototype = original.prototype; return newC; }
  13. Oui… mais niveau Runtime var __decorate = function () {

    … }; function Injectable(target) { … } var DevFestService = (function () { function DevFestService() {...} DevFestService = __decorate([ Injectable ], DevFestService); return DevFestService; }());
  14. Oui mais… pourquoi dans Angular2 • Définir les éléments Angular2

    • Injection de Dépendances • Créer des Inputs / Outputs • Accéder aux éléments HTML • Intéragir avec l’élément host
  15. Oui mais… pourquoi dans Angular2 //<button click-handler>Click Me</button> @Directive({ selector:

    ‘[click-handler]’}) export class ClickHandler { @Input() parameter: string; constructor(@Inject(LOCALE_ID) locale) {} @HostBinding(‘click’) click(){ … } }
  16. Le problème... startTimer() goToBordeaux(); setTimeout( _ => { veryLongTask() },

    0); setTimeout( _ => { veryLongTask() }, 0); goBackToLille(); endTimer()
  17. ZoneSpec interface ZoneSpec { onScheduleTask: () => {}, onInvokeTask: ()

    => {}, onHandleError: () => {}, onHasTask: () => {} }
  18. Monkey Patch window.setTimeout = function(c, time){ setTimeout(function(){ c(); }, time);

    } Zone.current.onScheduleTask(); Zone.current.onInvokeTask(); try { } catch (e){ Zone.current.onHandleError() }
  19. Désactivation des zones export class AnalyticsService { constructor(private zone:NgZone,private http:

    Http){} sendToAnalitics(stats){ this.zone.runOutsideAngular(() => { this.http.post(“/api/analytics”, stats); }); } }