CS5220 Advanced Topics in Web Programming Angular – Services

Slides:



Advertisements
Similar presentations
© 2014 IBM Corporation Empowering the IBM ecosystem Introduction to Developing applications using node.js on Bluemix IBM Ecosystem Development Instructors.
Advertisements

Web Integration to an Appx Backend Server. Unix web servers + CGI Win2K web servers + ASP Win2K web servers + ODBC Processing requests Generating HTML.
Intro To JMeter Christian Desserich Testing a Web-Based Application.
AJAX Without the “J” George Lawniczak. What is Ajax?
Krishna Mohan Koyya Glarimy Technology Services
2/26/021 Pegasus Security Architecture Author: Nag Boranna Hewlett-Packard Company.
INTEGRATION OF BACKBONE.JS WITH SPRING 3.1. Agenda New Features and Enhancements in Spring 3.1 What is Backbone.js and why I should use it Spring 3.1.
Spring RabbitMQ Martin Toshev.
1 Java Server Pages A Java Server Page is a file consisting of HTML or XML markup into which special tags and code blocks are inserted When the page is.
DICOMwebTM 2015 Conference & Hands-on Workshop University of Pennsylvania, Philadelphia, PA September 10-11, 2015 DICOMweb Workflow API (UPS-RS) Jonathan.
Authorization PDP GE Course (R4) FIWARE Chapter: Security FIWARE GE: Authorization PDP FIWARE GEri: AuthZForce Authorization PDP Owner: Cyril Dangerville,
CS520 Web Programming Spring – Web MVC Chengyu Sun California State University, Los Angeles.
CS520 Web Programming Spring – Inversion of Control Chengyu Sun California State University, Los Angeles.
CS320 Web and Internet Programming Handling HTTP Requests Chengyu Sun California State University, Los Angeles.
ArcGIS for Server Security: Advanced
CS3220 Web and Internet Programming Introduction to Java Servlets
Services & Dependency Injection
WWU Hackathon May 6 & 7.
CS5220 Advanced Topics in Web Programming Introduction to WebSocket
CS320 Web and Internet Programming Generating HTTP Responses
Node.js Express Web Services
CS520 Web Programming Spring – Inversion of Control
Chengyu Sun California State University, Los Angeles
J2EE Lecture 7: Spring – Spring MVC
Build Better Apps with MEAN.
CS5220 Advanced Topics in Web Programming Course Overview
Angularjs Interview Questions and Answers By Hope Tutors.
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming Spring – Web MVC
CS320 Web and Internet Programming Cookies and Session Tracking
CS5220 Advanced Topics in Web Programming Angular – Routing
CS320 Web and Internet Programming MVC Architecture
CS5220 Advanced Topics in Web Programming JavaScript Basics
CS5220 Advanced Topics in Web Programming Spring – Web MVC
HTTP GET vs POST SE-2840 Dr. Mark L. Hornick.
Lecture 5: Functions and Parameters
CS3220 Web and Internet Programming Introduction to Java Servlets
CS3220 Web and Internet Programming Handling HTTP Requests
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming Angular – TypeScript
CS5220 Advanced Topics in Web Programming Angular – Routing
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming More Node.js
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Web API with Angular 2 Front End
Azure Active Directory
Angular 2 Michael C. Kang.
CS5220 Advanced Topics in Web Programming Secure REST API
Leveraging ColdSpring To Make Better Applications
CS4961 Software Design Laboratory Understand Aquila Backend
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming Angular – TypeScript
CS5220 Advanced Topics in Web Programming Course Overview
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
CS5220 Advanced Topics in Web Programming Angular – Services
CS5220 Advanced Topics in Web Programming More Node.js
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
Chengyu Sun California State University, Los Angeles
SDC BL and Titan overview
CS4540 Special Topics in Web Development Course Overview
Chengyu Sun California State University, Los Angeles
Veterans Health Administration
Presentation transcript:

CS5220 Advanced Topics in Web Programming Angular – Services Chengyu Sun California State University, Los Angeles

Services Classes that implement business logic of an application Angular can make service classes available to other parts of the application via dependency injection

How Dependency Injection Works Declare something to be injectable Determine where to inject Have some mechanism to perform injection Injectables Inject Recipients

Dependency Injection in Spring Any bean can be injected into other beans Annotations, e.g. @Component Bean configuration file, e.g. <bean> Any bean can receive injection Auto wiring (default by type) Bean configuration file, e.g. property and ref Injection means Field, Setter, and Constructor

Dependency Injection in Angular Injectables (usually service classes and data) are either declared in providers of @NgModule, or decorated with @Injectable Injection via constructor Auto wiring by token

Declare Injectables in @NgModule Type Token providers: [ { provide: UserService, useClass: UserService }, { provide: 'SECRET', useValue: 'abcd' } ] String Token providers: [ UserService, { provide: 'SECRET', useValue: 'abcd' } ]

Declare Injectables Using @Injectable Inject using the application's root injector @Injectable({ providedIn: 'root' }) export class UserService { … } Angular's dependency injection system is hierarchical – injection can be done at the root, module, and component level

Constructor Injection Class HomeComponent { constructor( private UserService: userService, @Inject('SECRET') private secret: string ) {} }

Example: GuestBook … App Component entries[] @Input @Output GuestBook AddEntry Component

… Example: GuestBook App Component GuestBook Component AddEntry REST API Data Service

GuestBook Backend A simple Node.js server application generated using Express Generator Use a global array to hold GuestBook entries Two endpoints at /api/guestbook GET: get all entries POST: add an entry

Set Up Proxy During Development … NG Server at Port 4200 Angular App Browser REST API Call API Server at Port 3000 (or 8080) For security reasons, browsers enforce same-origin policy on XMLHttpRequest Localhost

… Set Up Proxy During Development … NG Server at Port 4200 Angular App REST API Call Browser API Server at Port 3000 (or 8080) Localhost

… Set Up Proxy During Development Create proxy configuration file proxy.conf.json, e.g. { "/api": { "target": "http://localhost:3000", "secure": false } Change start script in package.json ng serve --proxy-config proxy.conf.json

Create DataService Use Angular CLI to generate a service ng generate service <name> Naming conventions Inject it to component classes

Use HttpClient to Access REST API Import HttpClientModule from @angular/common/http Provides an HttpClient service that can be used to make HTTP requests Inject HttpClient services into the service class that will use it to make REST API calls

Get GuestBook Entries from Server In DataService: ?? getEntries(): Observable<GuestBookEntry[]> { return this.http.get<GuestBookEntry[]>('/api/guestbook'); } Angular automatically converts the response body into an array of GuestBookEntry Send a GET request In GuestBookComponent: ngOnInit() { this.dataService.getEntries().subscribe(entries => { this.entries = entries; }); } ??

Observable and Subscription An Observable represents a value or a set of values that change over time A Subscription allows a subscriber to be notified whenever the value(s) of an observable changes

Asynchronous Programming Paradigms Callback Function for event handling Promise for something that eventually produces a single result Observable for continuously changing data / data stream

Implement AddEntry The server API returns the newly added entry, but how do we notify GuestBookComponent to update its display?

Implementation Strategy Keep a local entries array in DataService and make it observable Add the newly added entry to the local entries array after the POST call GuestBookComponent subscribes to the local entries array to automatically get the new data when the array changes

A Bit More RxJS Observer Observer Observable Subject … … Observer … … Observer A Subject is a special type of Observable that allows values to be multicasted to many Observers.

Some Subject Methods next(value) emits the next value error(err) emits an error complete() indicates that the data stream has ended and all subscribers are unsubscribed

BehaviorSubject What if some subscriber comes in late (i.e. after some values are already emitted)? BehaviorSubject is a type of Subject that keeps the latest value vs ReplaySubject

Readings Angular HttpClient Guide RxJS Overview