29
Modular JavaScript Derek Swingley @derekswingley github.com/swingley Matt Driscoll @driskull github.com/driskull

Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

Modular JavaScript Derek Swingley @derekswingley

github.com/swingley

Matt Driscoll @driskull

github.com/driskull

Page 2: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

Agenda

• Why?

• OOP in JS • Vanilla JS Classes • Modules, AMD and Dojo • Widgets • Walkthrough

Page 3: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

Classes? Modules?

Why?

Page 4: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

Object Oriented

• Abstraction • Encapsulation • Inheritance • Polymorphism

Page 5: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

JavaScript has no class

Page 6: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

Let’s make a Class

Page 7: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

But…

• Scope? • Multiple inheritance? • Dependency management?

Page 8: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

AMD Modules

Page 9: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

ArcGIS JS API Docs Returns the Map Class as a parameter

Page 10: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

Before AMD

Page 11: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

Asynchronous Module Definition (AMD)

• Fewer globals • Plays nice

- can be used with other AMD compliant code

• Improvements - Asynchronous loading - Dependency handling - Cross origin loading

Page 12: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

Dojo Loader

• define() - Creates a module

• require()

- Gets a module

Page 13: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

Using Custom Modules

Custom Name

URL Path

Page 14: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

Dojo Config HTML5 Data Attribute

Page 15: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

A Simple AMD Module

Page 16: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

Using The Simple Module

Page 17: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

AMD Classes

Page 18: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

Writing Classes

• Use “dojo/_base/declare” to create a class • declare(className, superclass, props);

Parameter Type Description className String The optional name of

the constructor superclass Function | Function[] May be null, a Function,

or an Array of Functions. props Object An object whose

properties are copied to the created prototype

Page 19: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

Simple Classes

* Named classes should only be created if they will be used with the Dojo parser.

Inherited Classes (Optional)

[“esri/Map”, ”my/Module”,…]

Global Namespace (Optional)

Page 20: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

AMD Widgets

Page 21: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

Dijits Are Classy Too

• Templates • Common lifecycles • Events

- Owning event handles - Custom widget events

• Properties that can be - Get - Set - Watched

Page 22: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

"dijit/_WidgetBase"

• Foundation class. • Inherit this class to create a widget. • Handles widget lifecycle methods.

Page 23: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

"dijit/_TemplatedMixin"

• Provide a string of HTML for the widget to use as a template • String may have

- Variable Substitution - ${property} - Attach points for nodes - data-dojo-attach-point

“theme” Property of widget

Reference to this node

Localized text

Page 24: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

"dojo/Evented"

• Base class allowing a developer to emit events • Users can listen with “dojo/on”

Page 25: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

"dojo/Stateful"

• Base class for getting, setting, and watching for property changes • Inherited by "dijit/_WidgetBase"

Page 26: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

Walk-through Example

https://github.com/driskull/arcgis-dijit-home-button-js

Page 27: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

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

Page 28: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency
Page 29: Modular JavaScript · Asynchronous Module Definition (AMD) • Fewer globals • Plays nice -can be used with other AMD compliant code • Improvements -Asynchronous loading -Dependency

Derek Swingley @derekswingley

Matt Driscoll @driskull

Session survey: http://esriurl.com/7517