## Andy Gup (1)
ArcGIS API for JavaScript: Building apps for Angular
Andy Gup
@agup
Allison Davis
@araedavis
## What's the plan today? - Learn how to use ArcGIS API for JavaScript with Angular CLI - ArcGIS API for JavaScript modules with esri-loader - Async patterns - Angular Component Dev Kit (CDK) - Services - Routing - State management - ArcGIS API for JavaScript modules with webpack
## Angular 6+ ## ArcGIS API for JavaScript 4.x ![angular logo](img/angular.png) ### https://github.com/Esri/angular-cli-esri-map --- ## _Not "AngularJS", "Angular v1", "Angular.js", etc._
## Getting started 1. Download our repo - https://github.com/andygup/angular-plus-arcgis-javascript-ds2020 2. Follow along as we work with Angular sample apps - ./sample_apps/app-scaffolding - ./sample_apps/esri-loader - ...and more...
## [App Scaffolding](https://github.com/andygup/angular-plus-arcgis-javascript-ds2020/sample_apps/app-scaffolding) - Basic building blocks - Angular CLI - "esri-loader" - `esri-map.component.ts`
## Allison Davis (2)
## [esri-loader](https://github.com/Esri/esri-loader) - Reliably load (or lazy load) ArcGIS modules using our AMD loader - Control which ArcGIS modules when/where you want, à la carte - It's EASY and very lightweight #### [Sample application](https://github.com/andygup/angular-plus-arcgis-javascript-ds2020/sample_apps/app-scaffolding)
## esri-loader ```typescript import { loadModules } from 'esri-loader'; // `loadModules` dynamically injects a <script> tag onto the page ``` ```typescript // Use Promise-based pattern loadModules([ 'esri/Map', 'esri/views/MapView', 'esri/Graphic' ]).then(([ EsriMap, EsriMapView, Graphic ]) => { // Any ArcGIS JavaScript code here }); ``` ```typescript // Or, use async/await pattern async initializeMap() { const [Map, MapView, Graphic]:any = await loadModules([ 'esri/Map', 'esri/views/MapView', 'esri/Graphic ]) .catch(err => { console.error('ArcGIS: ', err); }); } ```
## [Types for arcgis-js-api](https://github.com/andygup/angular-plus-arcgis-javascript-ds2020/sample_apps/types-for-arcgis-js-api) - [TypeScript](https://www.typescriptlang.org/index.html): "a typed superset of JavaScript that compiles to plain JavaScript" - Angular uses TypeScript as a primary language for app development - Esri provides type definitions for the ArcGIS API for JavaScript: ```bash npm install --save @types/arcgis-js-api ``` Additional TypeScript resources: - [Angular's TS config guide](https://angular.io/guide/typescript-configuration) - [Esri's TypeScript setup guide](https://developers.arcgis.com/javascript/latest/guide/typescript-setup/)
## Asynchronous Operations - Three ways to manage out of process, out of sequence operations: - Promises - Custom Events - RxJS Observables
## [Async: Promises](https://github.com/andygup/angular-plus-arcgis-javascript-ds2020/sample_apps/async-promises) ![asynchronous operations](img/async_promises.png)
## [Async: Events](https://github.com/andygup/angular-plus-arcgis-javascript-ds2020/sample_apps/async-events) ![asynchronous operations](img/async_events.png)
## RxJS and Observables [RxJS](https://rxjs.dev/guide/overview) (Reactive Extensions for JavaScript) - a library for reactive programming Angular uses observables via RxJS to handle many common async operations: - [HTTP](https://angular.io/guide/observables-in-angular#http) - [Reactive forms](https://angular.io/guide/observables-in-angular#reactive-forms) - [Router](https://angular.io/guide/observables-in-angular#router) - Under the hood, [Angular custom events extends the RxJS Subject](https://angular.io/guide/observables-in-angular#transmitting-data-between-components)
## [Async: Observables](https://github.com/andygup/angular-plus-arcgis-javascript-ds2020/sample_apps/async-observables) ```typescript this.formSubscription = this.wonderForm.valueChanges.subscribe(value => { // do something cool with our updated form value! }) ``` ```typescript // don't forget to clean up any subscriptions ngOnDestroy() { this.formSubscription.unsubscribe() } ```
## Angular Component Developer Kit (CDK) - Authored by the Angular team - Implements common interaction patterns and components - Unopinionated about presentation - [Angular Material](https://material.angular.io/) built on top of CDK
## [Angular CDK demo](https://github.com/andygup/angular-plus-arcgis-javascript-ds2020/sample_apps/angular-cdk) - Accessibility and responsiveness work out of the box - Developers have full control - custom styles, sorting, pagination can be added on top - Table template and data source exist independently of each other
# Andy Gup (3)
## Angular State Management * Application state * Component state * Shared state * Router state ## Oh so many choices...! * RxJS * RxJS + Redux * NgRx * NGXS * angular-redux * ?
## [Simple map state with RxJS](https://github.com/andygup/angular-plus-arcgis-javascript-ds2020/sample_apps/simple-map-state) - Need to maintain state as we change routes - Can use a service and RxJS to maintain the state ![app screenshot](img/app_screenshot_2.png)
## [NgRx Data Store](https://github.com/andygup/angular-plus-arcgis-javascript-ds2020/tree/master/sample_apps/data-store) - Scalable approach to maintaining application state ![app screenshot](img/app_screenshot_3.png)
# Andy Gup (4)
## Angular + @arcgis/webpack-plugin https://github.com/Esri/angular-cli-esri-map/tree/arcgis-webpack-angular * Angular 8 and 9 🙄 * ArcGIS API for JavaScript 4.7+ only * Configure webpack using builders * angular-builders/custom-webpack https://www.npmjs.com/package/@angular-builders/custom-webpack
## @arcgis/webpack-plugin ## @angular-builders/custom-webpack Customize build config without ejecting webpack ```js // angular.json - custom configuration "build": { "builder": "@angular-builders/custom-webpack:browser", "options": { "customWebpackConfig": { "path": "./extra-webpack.config.js" }, . . . "serve": { "builder": "@angular-builders/custom-webpack:dev-server", "options": { "browserTarget": "angular-cli-esri-map:build" }, . . . ```
## extra-webpack.config.js ```js const ArcGISPlugin = require('@arcgis/webpack-plugin'); /** * Configuration items defined here will be appended to the end of the existing webpack config defined by the Angular CLI. */ module.exports = { plugins: [new ArcGISPlugin()], node: { process: false, global: false, fs: "empty" }, devtool: 'eval' } ```
## Initialize the Map ```js import Map from "esri/Map"; import MapView from "esri/views/MapView"; ngOnInit() { this._map = new Map({ basemap: this._basemap }); this._view = new MapView({ container: this.mapViewEl.nativeElement, map: this._map }); } ```
## Working with secure services ```javascript DOMException: Failed to execute 'importScripts' on 'WorkerGlobalScope' ``` https://github.com/Esri/arcgis-webpack-plugin#usage
## Resources - [Presentation GitHub repo](https://github.com/andygup/angular-plus-arcgis-javascript-ds2020) - [ArcGIS API for JavaScript](https://developers.arcgis.com/javascript/) - [Using the ArcGIS API for JavaScript with Angular: Guide](https://developers.arcgis.com/javascript/latest/guide/angular/) - [esri-loader](https://github.com/esri/esri-loader) - [arcgis-webpack-plugin](https://github.com/Esri/arcgis-webpack-plugin) - [Sample ArcGIS JS API/Angular CLI application](https://github.com/Esri/angular-cli-esri-map) - [GeoNet: ArcGIS API for JavaScript Community](https://community.esri.com/community/developers/web-developers/arcgis-api-for-javascript) - [GIS Stack Exchange](https://gis.stackexchange.com/)
Thank you
Andy Gup
@agup
Allison Davis
@araedavis