Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder...

34
Angular 2 and Observables The Zan and Jayna of Reactive Component Development

Transcript of Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder...

Page 1: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

Angular2andObservables

TheZanandJaynaofReactiveComponentDevelopment

Page 2: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

Justincaseyoudon'tknow...

Page 3: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

TheWonderTwinsofAngular2are

Components

RxJSObservables

TheygotogetherlikeChocolateandPeanutButter

Page 4: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

He'sCorny,WhoisThisGuy?

KenRimple-DirectorofTraining,ChariotSolutions

Long-timeJavaguy,Spring,nowintoJavaScript...TeachingandconsultingatChariotfor9+yearsAuthorofvariousAngularJScoursesatChariotCo-authorofAngular2andReactcoursesatChariotFatherof4andinveteratebadpunster

Page 5: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

Wheretofindme

Onthetweeterat@krimpleChariotTechCast/DevNewspodcasthostWritearticlesatchariotsolutions.com/blogaboutJSframeworksandmore

Page 6: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

Angular2is

AComponent-basedJavaScriptFramework...girdedbyaFunctionalReactiveAPI,Microsoft'sRxJS5.0

Page 7: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

WhatisaComponent?

@Component({ selector: 'message-area', template: `... html content here`, ...})export class MessageAreaComponent { constructor() { } message: string; // state variables doSomething() { ... } // user-defined methods ngOnInit() { ... } // lifecycle methods}

Page 8: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

You'reneverwalkingalone...

Theapplicationisbootstrappedwithanoutercomponent

Page 9: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

Acomponentinusebyanoutercomponent

// in the template of the outer component...

<message-area messageText="Don't walk and chew gum" [messageOptions]="messageOpts" (messageAccepted)="clearMessageText()"></message-area>

We'lllookateachoftheseassignments...

Page 10: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

Staticassignment

messageText="Don't walk and chew gum"enabled="true"

UsesthepropertyName="value"syntax,andexpectsliteralvaluesUsedforsimpleoptions

Page 11: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

Propertybinding

[propertyName]="variableOrExpression"

LooksinthecurrentcomponentforamembervariableorevaluatesanexpressionThevalueisassignedbyreferencetothepropertypropertyNameintheinnercomponent

Page 12: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

EventBinding

(eventName)="methodCall()"

Whenthemessage-areacomponentemitsthenamedevent,executethemethodmethodCall()intheoutercomponent

Page 13: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

DifferencesfromAngularJs1.x?

(Tons)

Nomore$scope,controllerStillhaveServices,thoughcreateddifferentlyStillhave"filters"butthey'recalledPipesnowDependenciesinjectedviaconstructorinjectionTypeScriptclassesinsteadofplainES5TheoldmessagebusisreplacedbyRxJSPromisesnowuseES2015PromisesyntaxorarereplacedbyRxJSobservables

Page 14: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

ComponentDemos

Page 15: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

ComponentsandObservables

Page 16: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

WhatisanObservable?

AnabstractioninRxJSthatcanactasadatastreamemitstypedinformationnotifiessubscriberswhentheinformationchanges

Page 17: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

Thisisafunctional,reactiveframework,

enabledbyComponentsandRxJS...

Page 18: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

Observablescanbeoperatedonfunctionally

ComposestreamsofobservableeventsChainoneoperatortoanotherTransformthestreamtowhatyouwantAllowformorethanonesubscriber

Page 19: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

KeyObservableType-EventEmitter

UsedinUItoreporteventsfromonecomponenttoanotherBoundusingthe@Outputannotation

@Component({ ...})export class ReportingComponent { @Output() onStatusChanged: EventEmitter<string> = new EventEmitter<string>();}

Page 20: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

Emittinganevent

@Component({ ... template: ` <button (click)='emitEvent()'>go!</button> `})...export class ReportingComponent { ... emitEvent() { this.onStatusChanged.emit('OUCH!'); }}

Page 21: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

Acceptinganemittedevent

Inject@Inputparametersusingeventsyntaxandsubscribeinyourcode

in template:<reporting (onStatusChanged)= "addStatusToHistory($event)"></reporting>

Page 22: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

Demo-emittinganeventfromaninput

field

Page 23: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

HttpAPIinAngular1.x-PromiseBased

// fetching data - APIreturn http.get('/api/customers') .then( (response) => { return response.data; } });

// callerservice.getData() .then((data) { this.data = data; });

Page 24: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

Angular2HttpAPIusesObservables

// fetching data - service return http.get('/api/customers') .map((res) => { res.json() })

// fetching data - callerservice.getData().subscribe( (results) => { this.results = results; } (error) => { ... });

Page 25: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

ObservableupdatesforHttp

// updateshttp.post('/api/customers', {}) .then( () => { /* leave location */ }, (error) => { /* show error */ });

Page 26: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

Demo-REST

Page 27: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

KeyObservableType-Subject

ActsasanObservableandanObserverMeaningyoucanuseittoemiteventsinastreamNottiedtoanUIcomponent

// creating - stream of arrays of stringslet dataStream = new Subject<string[]>();

// emitting - send an array chunkdataStream.next(['b', 'c', 'asdfkalsjdf']);

// subscribingdataStream.subscribe( (payload) => { console.log(payload); }, (error) => { alert('error with stream');}, () => { console.log('stream closed'); });

Page 28: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

PotentialSubjectStrategies

UseasanadaptertotranslatedatafromanotherAPIMidiInputUser-definedevents

GreattodecouplesourcesandsinksofdataDataissentasynchronouslyAngularusesthisinternallyExampleinRxJS-Observable.webSocket

Page 29: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

Demo-SynthesizerinAngularandRxJS

Page 30: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

AdvancedFeaturesofObservables-Operators

Observablestreamscanbecomposedandoperatedonwith

Higher-orderfunctionslikemap,debounce,filter,etc..Dozensbuilt-intoRxJS5.0-see forexamples

ObservableAPI

Page 32: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

Summary

Page 33: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

Angular2isPowerful

ButyouneedtolearnObservablestomakeitreactiveYouneedtomasterComponentsand@Inputand@OutputtocommunicateLookateachAngularAPIandlearnhowtheObservablesareused

Page 34: Angular 2 and Observablesfiles.meetup.com/19844279/Angular-2-talk-from-2016-10-17.pdf · The Wonder Twins of Angular 2 are Components RxJS Observables They go together like Chocolate

ResourcesandQ&ALookonTwitterat fortheslidedeckandcodesampleslatertonightChariot'sAngularandReacttrainingcourses,aswellasScala

Ken'sblogarticlesaboutAngular,aswellasthosefromotherCharioteersonScala,React,Mobileandmore-

WehaveaPhillyETEvideowatchingpartycomingup-

@krimple

chariotsolutions.com/training

chariotsolutions.com/blog

chariotsolutions.com/events