Modular JavaScript. JC Franco. Blake Stearman

Similar documents
Building Your own Widget with ArcGIS API for JavaScript

Deep Dive on How ArcGIS API for JavaScript Widgets Were Built

Advanced JavaScript. Gary Sheppard & James Tedrick

jmaki Overview Sang Shin Java Technology Architect Sun Microsystems, Inc.

Creating Custom Dojo Widgets Using WTP

JavaScript: the language of browser interactions. Claudia Hauff TI1506: Web and Database Technology

RequireJS Javascript Modules for the Browser. By Ben Keith Quoin, Inc.

Session 5. Web Page Generation. Reading & Reference

Static Webpage Development

JavaScript Lecture 1

Introduction to JavaScript p. 1 JavaScript Myths p. 2 Versions of JavaScript p. 2 Client-Side JavaScript p. 3 JavaScript in Other Contexts p.

JavaScript CS 4640 Programming Languages for Web Applications

Webpack. What is Webpack? APPENDIX A. n n n

Comprehensive AngularJS Programming (5 Days)

ArcGIS for Developers: An Introduction. Moey Min Ken

The course is supplemented by numerous hands-on labs that help attendees reinforce their theoretical knowledge of the learned material.

JavaScript CS 4640 Programming Languages for Web Applications

JQUERYUI - WIDGET FACTORY

Stamp Builder. Documentation. v1.0.0

Cascading Style Sheets Level 2

INF5750. Introduction to JavaScript and Node.js

Advanced React JS + Redux Development

UNIT -II. Language-History and Versions Introduction JavaScript in Perspective-

INTRODUCTION TO CSS. Mohammad Jawad Kadhim

Introduction to AngularJS

JavaScript Patterns O'REILLY* S toy an Stefanov. Sebastopol. Cambridge. Tokyo. Beijing. Farnham K8ln

JavaScript: Introduction, Types

Web Development for Dinosaurs An Introduction to Modern Web Development

Advance Mobile& Web Application development using Angular and Native Script

Basic Steps for Creating an Application with the ArcGIS Server API for JavaScript

Quick.JS Documentation

Building Backbone Plugins

Angular 2 and TypeScript Web Application Development

Web Application Development Using Spring, Hibernate and JPA

TypeScript coding JavaScript without the pain

Comm 244 Week 3. Navigation. Navigation. Websites need a formalized system of links to allow users to navigate the site

Web Application Development Using Spring, Hibernate and JPA

widgetjs Documentation

JavaScript. What s wrong with JavaScript?

ArcGIS App Strategies Ben

Create-A-Page Design Documentation

55249: Developing with the SharePoint Framework Duration: 05 days

Frontend UI Training. Whats App :

CISC 1600 Lecture 2.4 Introduction to JavaScript

Making Sling Grunt Or How to Integrate Modern Front-End Development with Sling. Philip Hornig (Publicis Pixelpark), Michael Sunaric (Netcentric)

Contents. Acknowledgments

Frontend Frameworks. SWE 432, Fall 2016 Design and Implementation of Software for the Web

JavaScript. The Bad Parts. Patrick Behr

Stencil: The Time for Vanilla Web Components has Arrived

AngularJS Fundamentals

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Laravel

Web Application Development Using Spring, Hibernate and JPA

Vue.js Framework. Internet Engineering. Spring Pooya Parsa Professor: Bahador Bakhshi CE & IT Department, Amirkabir University of Technology

Web development using PHP & MySQL with HTML5, CSS, JavaScript

Part of this connection identifies how the response can / should be provided to the client code via the use of a callback routine.

AngularJS AN INTRODUCTION. Introduction to the AngularJS framework

Etanova Enterprise Solutions

Developing Microsoft.NET Applications for Windows (Visual Basic.NET)

CSS.

Front End Programming

PROFESSIONAL TRAINING

TUTORIAL: D3 (1) Basics. Christoph Kralj Manfred Klaffenböck

Data Visualization (CIS/DSC 468)

Navigation. Websites need a formalized system of links to allow users to navigate the site

welcome to BOILERCAMP HOW TO WEB DEV

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

All India Council For Research & Training

AngularJS. Beginner's guide - part 1

FROM 4D WRITE TO 4D WRITE PRO INTRODUCTION. Presented by: Achim W. Peschke

Financial. AngularJS. AngularJS.

In this exercise we shall be using jsfiddle.net to build a simple data driven web site in HTML5. Building Blocks

Chapter 8: Using Toolbars

Working with JavaScript

Google Web Toolkit Creating/using external JAR files

Financial. AngularJS. AngularJS. Download Full Version :

Experimental New Directions for JavaScript

Web Development. with Bootstrap, PHP & WordPress

Tapestry. Code less, deliver more. Rayland Jeans

Web UI. Survey of Front End Technologies. Web Challenges and Constraints. Desktop and mobile devices. Highly variable runtime environment

Nick Senger & Jesse van den Kieboom

Front End. Presentation Layer. UI (User Interface) User <==> Data access layer

JavaScript Rd2. -Kyle Simpson, You Don t Know JS

Oracle WebLogic Portal

JavaScript (5A) JavaScript

0.9: Faster, Leaner and Dijit? July 25, 2007 Dylan Schiemann. presented by

Unveiling the Basics of CSS and how it relates to the DataFlex Web Framework

The Next Generation of Eclipse: e4. Mike Milinkovich Executive Director Eclipse Foundation

Getting Started with the ArcGIS Server JavaScript API

web-sockets-homework Directions

Building Native Mapping Apps with PhoneGap: Advanced Techniques Andy

VS005 - Cordova vs NativeScript

Native Mobile Application Development Ecosystem

CGS 3066: Spring 2015 JavaScript Reference

QOOMdOO. 1 open source I community experience distilled. Beginner's Guide. Develop Rich Internet Applications (RIA) with qooxdoo. Rajesh Kumar Bachu

Web AppBuilder for ArcGIS Dive Into Mobile Development. Yiwei Ma & Kevin Gao

New Face of z/os Communications Server: V2R1 Configuration Assistant

The Dojo Ecosystem: How Open-Source and Commercial Succeed Together


Node.js. Node.js Overview. CS144: Web Applications

PHP WITH ANGULAR CURRICULUM. What you will Be Able to Achieve During This Course

Transcription:

Modular JavaScript JC Franco Blake Stearman

console.log("hello world"); JC Franco @arfncode jcfranco Blake Stearman @cthru BlakeStearman

The Story of an App We ve all been here. Start small Add features Grow complexity Add developers Trouble <!DOCTYPE html> <html> <head lang="en"> <meta charset="utf-8"> </head> <body> </body> </html>

The Story of an App We ve all been here. Start small Add features Grow complexity Add developers Trouble <!DOCTYPE html> <html> <head lang="en"> <meta charset="utf-8"> <title>stopwatch</title> </head> <body> <div> <div id="timer">00:00:00:000</div> </div> </body> </html>

The Story of an App We ve all been here. Start small Add features Grow complexity Add developers Trouble <!DOCTYPE html> <html> <head lang="en"> <meta charset="utf-8"> <title>stopwatch</title> </head> <body> <div> <div id="timer">00:00:00:000</div> <button type="button" id="start-button">start</button> <button type="button" id="stop-button">stop</button> </div> </body> </html>

The Story of an App We ve all been here. Start small Add features Grow complexity Add developers Trouble <!DOCTYPE html> <html> <head lang="en"> <meta charset="utf-8"> <title>stopwatch</title> <link rel="stylesheet" href="./css/app.css"> </head> <body> <div class="nav"> <a class="hoverhighlight" href="./index.html">< Back</a> Stopwatch script </div> <div class="display"> <div id="timer">00:00:00:000</div> <button type="button" id="start-button">start</button> <button type="button" id="stop-button">stop</button> </div> </body> </html>

The Story of an App We ve all been here. Start small Add features Grow complexity Add developers Trouble <!DOCTYPE html> <html> <head lang="en"> <meta charset="utf-8"> <title>stopwatch</title> <link rel="stylesheet" href="./css/app.css"> </head> <body> <div class="nav"> <a class="hoverhighlight" href="./index.html">< Back</a> Stopwatch script </div> <div class="display"> <div id="timer">00:00:00:000</div> <button type="button" id="start-button">start</button> <button type="button" id="stop-button">stop</button> </div> <script> //... </script> </body> </html>

Monolithic apps are Depressing

Modules Building blocks of code. What is a module? Cohesive Single-purpose Reusable Interchangeable Why use modules? Focused code Simplifies Reuse Testing Collaboration Clean global scope Promotes DRY Don t Repeat Yourself Don t Repeat Yourself Don t Repeat Yourself

JavaScript Module Pattern The foundation Immediately-Invoked Function Expression (IIFE) Truly private scope Executes immediately Can return value (function () { // your awesome code (⁰ ⁰) })();

Diving Into the Pattern var MyModule = (function () { Creates namespace Private data Expose API var mydata = 0; // private data function myprivatemethod () { // private - not exposed through API }; function mypublicmethod () { }; // public - exposed through API // MyModule API return { publicmethod: mypublicmethod }; })();

Now stop Demo Time!

Module Pattern Caveats Name collisions Loading order matters Truly private data - Limits extension // rewire Stopwatch.displayTime() to return raw time Stopwatch.displayTime = function () { return Stopwatch._time(); // _time() is private }; Stopwatch.displayTime(); // TypeError: undefined is not a function

Variation on Pattern To make our lives easier. Expose all Private as convention Improves - Debugging - Extension var MyModule = (function () { // MyModule API return { // private _data: 0, _privatemethod: function () { }, // public publicmethod: function () { } }; })();

Module Definitions Taking the module pattern to another level. Asynchronous Module Definition (AMD) CommonJS Universal Module Definition (UMD) ECMAScript 6 Harmony

Asynchronous Module Definition (AMD) // modulea.js Asynchronous loading Web-based solution Lazy loading Implementation - define - require define(["moduleb"], function (moduleb) { // module API return { _data: 100, calculate: function () { moduleb.calculate(this._data); } }; }); // main.js require(["modulea ], function (modulea) { modulea.calculate(); });

AMD // modulea.js define(["moduleb"], function (moduleb) { // module API return { _data: 100, calculate: function () { moduleb.calculate(this._data); } }; }); Look familiar? var modulea = (function (moduleb) { // module API return { _data: 100, calculate: function () { return moduleb.calculate(this._data); } }; })(moduleb);

CommonJS Desktop environment* Synchronous Can only export objects* // modulea.js var moduleb = require("moduleb"); var _data = 100; // module API (exports) exports.calculate = function() { moduleb.calculate(_data); }; // main.js var modulea = require("modulea"); modulea.calculate();

UMD Supports browser and desktop Boilerplate Variations // modulea.js (function () { define(["moduleb"], function (moduleb) {...}); })( // AMD or CommonJS environment? typeof define == "function" && define.amd? define : function (ids, factory) { var deps = ids.map(function (id) { return require(id); }); module.exports = factory.apply(null, deps); } ); define(["moduleb"], function (moduleb) { return function () { return { _data: 100, calculate: function () { moduleb.calculate(this._data); } }; }; });

ES6 Harmony ECMAScript standard Supports browser and desktop Coming to an interpreter near you // modulea.js import moduleb from "moduleb"; var _data = 100; export function calculate () { } return moduleb.calculate(_data); // main.js import modulea from "modulea"; modulea.calculate();

Frameworks The good, the bad and the ugly. The Good Structured Modular Scaffolding Documented (usually) The Bad Overhead Bugs Vulnerabilities The Ugly Mixing Interchanging

Dojo Toolkit http://dojotoolkit.org Foundation of ArcGIS JavaScript API AMD Class-based inheritance Internationalization Why Dojo? https://developers.arcgis.com/javascript/jshelp/why_dojo.html

Defining Modules in Dojo define declare* define(["dojo/_base/declare"], function (declare) { return declare(null, { _data: 100, getdata: function () { return this._data; } }); });

Defining Widgets in Dojo Extend _WidgetBase define([ "dojo/_base/declare", "dijit/_widgetbase", ], function (declare, _WidgetBase) { return declare([_widgetbase], { declaredclass: "MyWidget", _data: 100, getdata: function () { return this._data; } }); });

Dojo Widget What do you gain? Lifecycle constructor postmixinproperties buildrendering postcreate startup destroy

Dojo Templated Widget Putting a face on it. Extend _TemplatedMixin define([ "dojo/_base/declare", "dijit/_widgetbase", "dijit/_templatedmixin" ], function (declare, _WidgetBase, _TemplatedMixin) { return declare([_widgetbase, _TemplatedMixin], { declaredclass: "MyHelloWorld", templatestring: "<div><label>hello World!</label></div>" }); });

Dojo Templated Widget Continued Best practice - define template in separate file - import template file as a string <!--./templates/myhelloworld.html --> <div> <label>hello World!</label> </div> //./MyHelloWorld.js define([ "dojo/_base/declare", "dijit/_widgetbase", "dijit/_templatedmixin", "dojo/text!./templates/myhelloworld.html" ], function (declare, _WidgetBase, _TemplatedMixin, template) { return declare([_widgetbase, _TemplatedMixin, template], { declaredclass: "MyHelloWorld", templatestring: template }); });

Styling a Templated Widget Making it look pretty. Use CSS Inline CSS? Styling bound with template Harder to fix styling issues Best practice Define separate CSS file Namespace using class name Inject namespace class name Import your CSS file <div> <label style="font-size: 24px; Hello World! </label> </div> color: chartreuse;"> <div class="myhelloworld"> <label>hello World!</label> </div>.myhelloworld label { font-weight: bold; }

Styling Best Practice What should this look like? /*./css/myhelloworld.css */.myhelloworld label { font-weight: bold; } <!--./templates/myhelloworld.html --> <div> <label>hello World!</label> </div> //./MyHelloWorld.js define([ "dojo/_base/declare", "dijit/_widgetbase", "dijit/_templatedmixin", "dojo/text!./templates/myhelloworld.html" ], function (declare, _WidgetBase, _TemplatedMixin, template) { return declare("myhelloworld", [_WidgetBase, _TemplatedMixin, template], { baseclass: "myhelloworld", templatestring: template }); });

Revisiting Stopwatch Dojo Conversion

Recap Things to remember. Modular JavaScript - Break code into cohesive, reusable modules - Each has specific purpose - Each contains code only for that purpose Best practices - DRY - Separate files (code, template, styles) - Import templates using dojo/text - Namespace styles Dojo/Dijit Modules - dijit/_widgetbase - dijit/_templatedmixin - dijit/_widgetsintemplatemixin - dijit/a11yclick - dojo/evented - dojo/on -

Resources Online swag. Stopwatch demo files https://github.com/blakestearman/stopwatch-devsummit-2015 JavaScript Web Optimizer https://jso.arcgis.com/ Dijit - http://dojotoolkit.org/reference-guide/1.10/dijit/ - http://dojotoolkit.org/reference-guide/1.10/quickstart/writingwidgets.html - https://dojotoolkit.org/documentation/tutorials/1.10/recipes/custom_widget/ Books - Learning JavaScript Design Patterns by Addy Osmani - JavaScript Patterns by Stoyan Stefanov

Rate This Session www.esri.com/ratemydevsummitsession