1 / 26

Building Reusable Widgets with ArcGIS API for JavaScript

Building Reusable Widgets with ArcGIS API for JavaScript. Derek Swingley @derekswingley Matt Driscoll @driskull. DRY. How to stay DRY?. Classes. Modules. JavaScript has no class*. *Until we’re all using the next version of JavaScript. Let’s make a Class. But…. Scope?

lottie
Download Presentation

Building Reusable Widgets with ArcGIS API for JavaScript

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Building Reusable Widgets with ArcGIS API for JavaScript Derek Swingley @derekswingley Matt Driscoll @driskull

  2. DRY Building Reusable Widgets with ArcGIS API for JavaScript

  3. How to stay DRY? Classes. Modules. Building Reusable Widgets with ArcGIS API for JavaScript

  4. JavaScript has no class* *Until we’re all using the next version of JavaScript Building Reusable Widgets with ArcGIS API for JavaScript

  5. Let’s make a Class Building Reusable Widgets with ArcGIS API for JavaScript

  6. But… • Scope? • Multiple inheritance? • Dependency management? Building Reusable Widgets with ArcGIS API for JavaScript

  7. AMD • Manages globals • Compatible with other AMD loaders • Async dependency management Building Reusable Widgets with ArcGIS API for JavaScript

  8. The Future: http://jsmodules.io/ Building Reusable Widgets with ArcGIS API for JavaScript

  9. Dojo Widgets • Classes to inherit • Sample demo on Github • Help you write a widget Building Reusable Widgets with ArcGIS API for JavaScript

  10. dijit._WidgetBase • Lifecycle methods • Constructor, postMixInProperties, buildRendering, postCreate, startup, destroy • Properties/Attributes • Mix them in • Setters & Getters (Stateful) • Owning handles (events) • .own() Building Reusable Widgets with ArcGIS API for JavaScript

  11. dijit._Templated <div class="${baseClass}" data-dojo-attach-point="focusNode" data-dojo-attach-event="ondijitclick:_onClick" role="menuitem" tabindex="-1"> <span data-dojo-attach-point="containerNode"></span> </div> • Template String • Dom Construction • Access Nodes • Attach Points • data-dojo-attach-point • Event attachments • Variable Substitution • ${property} Building Reusable Widgets with ArcGIS API for JavaScript

  12. dojo/Stateful • Included with Widgetbase • Base class for getting, setting, and watching for property changes • get() & set() methods • Watch() • Custom getters & setters Building Reusable Widgets with ArcGIS API for JavaScript

  13. dojo/Evented • Emit Events • Users can listen with “dojo/on” this.emit("my-event", {}); myWidget.on("my-event", function(evt){}); Building Reusable Widgets with ArcGIS API for JavaScript

  14. Deferreds & Promises • dojo/promise • promise() • all() • first() • dojo/deferred Building Reusable Widgets with ArcGIS API for JavaScript

  15. Dojo/promise/promise • Communication between asynchronous threads • Can be in one of three states: unfulfilled, resolved, rejected • May only change from unfulfilled to resolved or unfulfilled to rejected • Methods • .then() • notification of state change • .cancel() • .always() • .otherwise() Building Reusable Widgets with ArcGIS API for JavaScript

  16. Dojo/promise/first • Takes an array or object of promises • Returns results from first that is fulfilled first([promise1, promise2]).then(function(result){ // result will be either promise1 or promise2 results, // whichever is fulfilled first }); Building Reusable Widgets with ArcGIS API for JavaScript

  17. Dojo/promise/all • Takes an array or object of promises • Returns new promise with results when all promises are fulfilled or one is rejected • Replaces dojo/DeferredList all([promise1, promise2]).then(function(results){ }); Building Reusable Widgets with ArcGIS API for JavaScript

  18. Dojo/deferred • Manages async threads • deferred.promise; • .resolve() / .reject() • dojo/request and esri/request use deferreds • Can be chained • .then() • .cancel() Building Reusable Widgets with ArcGIS API for JavaScript

  19. require(["dojo/Deferred", "dojo/dom", "dojo/on", "dojo/domReady!"], function(Deferred, dom, on){ function asyncProcess(){ var deferred = new Deferred(); dom.byId("output").innerHTML = "I'm running..."; setTimeout(function(){ deferred.resolve("success"); }, 1000); return deferred.promise; } on(dom.byId("startButton"), "click", function(){ var process = asyncProcess(); process.then(function(results){ dom.byId("output").innerHTML = "I'm finished, and the result was: " + results; }); }); }); Building Reusable Widgets with ArcGIS API for JavaScript

  20. Chained Deferred require(["dojo/Deferred"], function(Deferred){ var def = new Deferred(); def.then(function(value){ // do something return something; }).then(function(something){ // do something else }); }); Building Reusable Widgets with ArcGIS API for JavaScript

  21. Demo Time: Building Reusable Widgets with ArcGIS API for JavaScript

  22. Get the code! github.com/esri arcgis-dijit-sample-js Building Reusable Widgets with ArcGIS API for JavaScript

  23. Tips & Tricks Building Reusable Widgets with ArcGIS API for JavaScript

  24. Tips • Refer back to the latest Dojo API • Some functions deprecated or replaced • Use newer Dojo functions if available • Use new event handling with on() • Get/Set widget properties with .get() and .set() • watch() widget properties and call update functions Building Reusable Widgets with ArcGIS API for JavaScript

  25. Thank you… Please fill out the session survey: Tue 7/15 Offering ID: 1529 Wed 7/16 Offering ID: 1858 Online – www.esri.com/ucsessionsurveys Building Reusable Widgets with ArcGIS API for JavaScript

  26. Building Reusable Widgets with ArcGIS API for JavaScript

More Related