widgetjs Documentation

Similar documents
JQuery WHY DIDN T WE LEARN THIS EARLIER??!

jquery Tutorial for Beginners: Nothing But the Goods

Building Your own Widget with ArcGIS API for JavaScript

React + React Native. Based on material by Danilo Filgueira

JQUERYUI - WIDGET FACTORY

COURSE OUTLINE MOC 20480: PROGRAMMING IN HTML5 WITH JAVASCRIPT AND CSS3

Lesson: Web Programming(6) Omid Jafarinezhad Sharif University of Technology

Installing VS Code. Instructions for the Window OS.

JavaScript and MVC Frameworks FRONT-END ENGINEERING

Interactor Tree. Edith Law & Mike Terry

Frontend II: Javascript and DOM Programming. Wednesday, January 7, 15

welcome to BOILERCAMP HOW TO WEB DEV

Theme System. Wisej Themes 1 OVERVIEW

Q2D8T#YH# BYUQTUQ+ *")#+,-./,01#2../,345,67#869,3#47:#+5;<35<;=!"""""""""""# $& B675;6//=;#CD/645E!

Templates and Databinding. SWE 432, Fall 2017 Design and Implementation of Software for the Web

Advanced React JS + Redux Development

React. HTML code is made up of tags. In the example below, <head> is an opening tag and </head> is the matching closing tag.

Best Practices Chapter 5

International Research Journal of Engineering and Technology (IRJET) e-issn: Volume: 05 Issue: 06 June p-issn:

WELCOME TO JQUERY PROGRAMMING LANGUAGE ONLINE TUTORIAL

Deep Dive on How ArcGIS API for JavaScript Widgets Were Built

the usual suspects UIObject HasWidgets Widget

CodeValue. C ollege. Prerequisites: Basic knowledge of web development and especially JavaScript.

THE PRAGMATIC INTRO TO REACT. Clayton Anderson thebhwgroup.com WEB AND MOBILE APP DEVELOPMENT AUSTIN, TX

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

Anatomy of a SPA: Client-side MVC

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

Angular 2 Programming

CSI 3140 WWW Structures, Techniques and Standards. Browsers and the DOM

Front-End Web Developer Nanodegree Syllabus

JavaScript. jquery and other frameworks. Jacobo Aragunde Pérez. blogs.igalia.com/jaragunde

JQUERYUI - PROGRESSBAR

HTML CSS JAVASCRIPT WEB PUBLISHING IN ONE HOUR A DAY SAMS TEACH YOURSELF COVERING HTML5 CSS3 AND JQUERY 7TH EDITION

High Performance Single Page Application with Vue.js

Advance Mobile& Web Application development using Angular and Native Script

Javascript Performance in the Browser. Charlie Fiskeaux II User Interface Engineer

JavaScript: the Big Picture

20480C: Programming in HTML5 with JavaScript and CSS3. Course Code: 20480C; Duration: 5 days; Instructor-led. JavaScript code.

Frontend UI Training. Whats App :

Web Development. with Bootstrap, PHP & WordPress

DATABASE SYSTEMS. Introduction to web programming. Database Systems Course, 2016

Microsoft Expression Web Quickstart Guide

jquery UI Widget Factory

Building Rich Web Applications using XForms 2.0

Static Webpage Development

jquery Cookbook jquery Community Experts O'REILLY8 Tokyo Taipei Sebastopol Beijing Cambridge Farnham Koln

jquery with Fundamentals of JavaScript Training

User Interaction: jquery

React.js. a crash course. Jake Zimmerman January 29th, 2016

Comprehensive AngularJS Programming (5 Days)

Widget ID Each user type widget should have a unique identifier within a single controller (ID). Any string can be as ID.

for Lukas Renggli ESUG 2009, Brest

Web Programming and Design. MPT Junior Cycle Tutor: Tamara Demonstrators: Aaron, Marion, Hugh

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

SIX REASONS. You Should Use ArcGIS API 4.0 for JavaScript. By Julie Powell, Esri Product Management

CS50 Quiz Review. November 13, 2017

Client Side JavaScript and AJAX

CSC Web Programming. JavaScript Browser Objects

Lab 1 - Introduction to Angular

Stencil: The Time for Vanilla Web Components has Arrived

Tooling for Ajax-Based Development. Craig R. McClanahan Senior Staff Engineer Sun Microsystems, Inc.

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

Adapt Learning: Adapt Framework Concept and Vision

"Charting the Course... Comprehensive Angular. Course Summary

Brunch Documentation. Release Brunch team

HTML CS 4640 Programming Languages for Web Applications

Course 20480: Programming in HTML5 with JavaScript and CSS3

COURSE 20480B: PROGRAMMING IN HTML5 WITH JAVASCRIPT AND CSS3

E , Fall 2007 More with stylesheets

TUTORIAL MADCAP FLARE Top Navigation

write less. do more.

Frontend Frameworks Part 2. SWE 432, Fall 2017 Design and Implementation of Software for the Web

Multimedia im Netz Online Multimedia Winter semester 2015/16

Sample CS 142 Midterm Examination

Welcome to CS50 section! This is Week 10 :(

PHP Online Training. PHP Online TrainingCourse Duration - 45 Days. Call us: HTML

Programming in HTML5 with JavaScript and CSS3

Learn Web Development CodersTrust Polska course outline. Hello CodersTrust! Unit 1. HTML Structuring the Web Prerequisites Learning pathway.

Web Development 20480: Programming in HTML5 with JavaScript and CSS3. Upcoming Dates. Course Description. Course Outline

Human-Computer Interaction Design

XAP: extensible Ajax Platform

Accessible Rendered Item (ARI) Technical Specifications. Prepared by: Ali Abedi, UCLA/CRESST Last updated: 4/11/2016

P a g e 1. Danish Technological Institute. Scripting and Web Languages Online Course k Scripting and Web Languages

CS142 Winter 2017 Midterm Grading Solutions and Rubric

IN4MATX 133: User Interface Software

Catbook Workshop 1: Client Side JS. Danny Tang

An Introduction to JavaScript & Bootstrap Basic concept used in responsive website development Form Validation Creating templates

Introduction to WEB PROGRAMMING

Using CSS for page layout

Participation Status Report STUDIO ELEMENTS I KATE SOHNG

Welcome Please sit on alternating rows. powered by lucid & no.dots.nl/student

Tutorial 2 Editor Brackets

CUSTOMER PORTAL. Custom HTML splashpage Guide

How to lay out a web page with CSS

Front End Nanodegree Syllabus

For purposes of demonstration we ll create a simple Hello World widget that accepts a bindable DisplayText and adds that text to Hello World.

Installation Guide. Sitecore Federated Experience Manager. Installation & Configuration Guide

Session 7. JavaScript Part 2. W3C DOM Reading and Reference

Frontend guide. Everything you need to know about HTML, CSS, JavaScript and DOM. Dejan V Čančarević

Introduction. Part I: jquery API 1. Chapter 1: Introduction to jquery 3

Transcription:

widgetjs Documentation Release 1.2.3 Nicolas Vanhoren Dec 22, 2017

Contents 1 Tutorial 3 1.1 Presentation of widgetjs......................................... 3 1.2 Quickstart................................................ 3 1.3 A First Widget.............................................. 4 1.4 The Widget s Root Element....................................... 4 1.5 Appending Widgets Into The DOM................................... 5 1.6 Widget Events.............................................. 6 1.7 DOM Events............................................... 6 1.8 Widget Life Cycle............................................ 7 1.9 Putting It All Together.......................................... 8 1.10 Tools and Shortcuts........................................... 8 i

ii

This is the documentation of widgetjs_, the lightweight JavaScript widget framework. Contents: Contents 1

2 Contents

CHAPTER 1 Tutorial 1.1 Presentation of widgetjs widgetjs is a lightweight framework in JavaScript to separate web applications in multiple reusable components called widgets. widgetjs is not a full featured framework that can handle all aspects of a web application like network communications or routing, there already exists good libraries for that. widgetjs only handles one aspect of web development: separation of visual components into independant entities. So it provides only features to serve that goal, namely widgets, lifecycle management and events. 1.2 Quickstart The easiest way to start a widgetjs application is to checkout the sample application. Using git, do this: git clone https://github.com/nicolas-van/widgetjs-starter.git -b 1.2.3 This sample application uses npm and grunt to download the dependencies and launch a small web server. Type these lines to download everything and start the server: npm install grunt Then head your web browser to http://localhost:9000 and you will see the Hello World message outputed by the application. 1.2.1 A Word About Class Inheritance widgetjs allows to use ES6 class inheritance on its classes like this: 3

class MyWidget extends widgetjs.widget { We recommend to use this ES6 feature and to use Babel when you need to support old browsers (which is what the sample application does). But if you don t want or can use Babel we also provide the extend() helper on all classes to simplify inheritance: var MyWidget = widgetjs.widget.extend({ constructor: function() { widgetjs.widget.call(this); ); The provided examples below all use ES6 syntax, just know that everything is usable with extend() too. 1.2.2 A Word About Template Engines widgetjs is a lightweight framework. As such, it doesn t impose a particular template engine to render HTML. The sample application uses Nunjucks from Mozilla as it is a high quality and full featured template engine in JavaScript, but you are free to replace it by any other template engine. 1.3 A First Widget Take a look at the src/js/app.js file to have an example of your first widget: myapp.widget1 = class Widget1 extends widgetjs.widget { this.el.innerhtml = nunjucks.render('widget1.html'); We can see that Widget1 is a simple subclass of widgetjs.widget. It overrides the constructor to add some HTML to the root element of the widget, this.el. In this example that HTML is rendered using Nunjucks (the widget1.html file is located in the views folder). In the index.html file we can see how this widget is instantiated and appended into the DOM: var widget1 = new myapp.widget1(); widget1.appendto(document.body); The widget is instantiated and then we call the appendto() method by passing an element. appendto() is one of the multiple methods allowing to manipulate the location of widgets. 1.4 The Widget s Root Element When a widget is created, its root element is created with it. By default it s always a <div> but it s possible to change that behavior. 4 Chapter 1. Tutorial

You can access the root element of a widget by using the el accessor: class MyWidget extends widgetjs.widget { this.el.innerhtml = "<p>hi, I'm a widget!</p>"; console.log(new MyWidget().el); // Prints this element: // <div> // <p>hi, I'm a widget!</p> // </div> The generation of the root element can be customized using the tagname, attributes and classname attributes: class MyWidget extends widgetjs.widget { get tagname() { return "span"; get classname() { return "mywidget"; get attributes() { return {"style": "display: block"; this.el.innerhtml = "<p>hi, I'm a widget!</p>"; console.log(new MyWidget().el); // Prints this element: // <span class="mywidget" style="display: block"> // <p>hi, I'm a widget!</p> // </span> 1.5 Appending Widgets Into The DOM By instanciating a widget you initialize it with its root element. But it s still detached from the DOM. To insert it into the DOM you can use one of the methods like appendto(): class MyWidget extends widgetjs.widget { this.el.innerhtml = "<p>hi, I'm a widget!</p>"; new MyWidget().appendTo(document.body); The appendto() method appends the root element at the end of the provided element. Multiple other methods exist to serve the same purpose, with some differences regarding the place where the root element will be inserted: appendto() prependto() insertafter() insertbefore() replace() 1.5. Appending Widgets Into The DOM 5

detach() (this one removes the widget from the DOM) Warning: It is not recommended to directly alter the placement of the root element by using el. Doing so will disable some of widgetjs s features that will be explained later. 1.6 Widget Events Events is one of the main features of widgetjs, and an incredibly useful tool in all modern UI libraries. Widget events are separate from DOM events like click or submit. They are used to define your own custom events. Example: class MyWidget extends Widget { dosomething() { // some code... this.trigger("someevent"); var x = new MyWidget(); x.on("someevent", function() { console.log("an event occured"); ); x.dosomething(); // prints "an event occured" on() is used to register event handlers, trigger() is used to trigger one and off() can be used to unregister if you need to. on() can also be used to register multiple events at once: x.on({ "someevent": function() {..., "someotherevent": function() {..., ); See also: If you want to use events outside of widgets you can use the widgetjs.eventdispatcher class. 1.7 DOM Events While it is perfectly feasible to call addeventlistener() on the root element accessed using el, widgetjs provides an easier way to listen to those DOM events: var x = new MyWidget(); x.on("dom:click", function() { console.log("the element was clicked"); ); When adding dom: at the beggining of the event type when calling on() you can proxy the DOM events through the widget. It is also possible to listen to events on sub elements of the root element: 6 Chapter 1. Tutorial

x.on("dom:click button", function(e) { // note that you can replace "button" by any CSS selector console.log("the button was clicked"); ); Doing so uses event bubbling. In this example the hypothetic button could be created after the call to on() without problems. It can also be great for performances in multiple cases. Also note that, in the above example, you can get the button element using e.bindedtarget. 1.8 Widget Life Cycle 1.8.1 Widget Destruction We saw how to create widgets, now it is time to destroy them. To do so just call the destroy() method: var x = new widgetjs.widget(); x.appendto(document.body); x.destroy(); // the root element of x has been removed from the DOM Once destroy() has been called on a widget it is considered a dead object. Its root element is detached and all its event handlers are removed. See also: Removing the event handlers when an widget is destroyed simplifies the task of the garbage collector as events tend to generate a lot of circular references that make objects removal difficult. It is also common to override the destroy() method to add some cleanup code. Remember: widgets are independant visual components. Aside from displaying HTML code they could encapsulate any kind of behavior like animations, network communication, etc... They are always susceptible to reserve ressources that should be freed or run background processes that should be stopped. 1.8.2 Parent-Children Relationship Widgets maintain a parent-children between themselves. You can see that relationship by using the parent and children attributes. class MyWidget1 extends widgetjs.widget { this.otherwidget = new MyWidget2().appendTo(this.el); class MyWidget2 extends widgetjs.widget { // another widget var x = new MyWidget1().appendTo(document.body); console.log(x.otherwidget.parent === x); // prints true console.log(x.children[0] === x.otherwidget); // prints true 1.8. Widget Life Cycle 7

Widgets maintain their parent-children automatically. You can also specify it explicitly by setting the parent attribute. When a widget is destroyed it will destroy its children recursively: x.destroy(); console.log(x.destroyed); // prints true console.log(x.otherwidget.destroyed); // prints true Life cycle management using parent-children relationship is useful in big applications where a lot of widgets contain other widgets. When relationship are correctly defined, whenever you destroy a widget all the widgets it created will be destroyed. By extension all ressources that were directly or indirectly reserved by that widget will also be freed. See also: If you want to use life cycle management outside of widgets you can use the widgetjs.lifecycle class. 1.9 Putting It All Together widgetjs is just a toolbox that gives some indications on how to define good components. It is still necessary to use common sense and good practices to create scalable and maintainable applications. Widgets should be considered as black boxes from the outside. A widget s HTML should only be modified by that same widget and be invisible from other components of the application. As example, only a widget should register DOM events on one of its own elements. If you have a widget containing a <form> element, never register the submit event from outside the widget by doing something like thewidget. on("dom:submit form",...). Here is a more correct way to do it: class MyWidget extends widgetjs.widget { this.on("dom:submit form", this.formsubmit); this.el.innerhtml = nunjucks.render('myform.html'); formsubmit() { this.trigger("formcompleted"); Here we forward the submit DOM event to a method that will trigger a formcompleted widget event. The difference is that the submit DOM event is only a technical detail about how a HTML <form> works. The formcompleted widget event is much more meaningful as a high level event: it identifies when the user has finished completing the form. If later we want to add validation to our widget, add complex asynchronous operations or transform the widget into something completely different like a wizard we can do so without modifying the external API of our widget. So any piece of code in our application that already used the MyWidget class will not see the difference. To sum it: MyWidget is a component that correctly encapsulates its behavior. 1.10 Tools and Shortcuts The previous parts of this tutorial presented the main features of widgetjs, but there are still many shortcuts that can be used to reduce the amount of code: 8 Chapter 1. Tutorial

1.10.1 Ready The typical helper to know if the browser finished the loading of the page, if you don t plan to use jquery: widgetjs.ready(function() { // put some code ); 1.10.2 Standard Widget Events Some events are automatically triggered by widgets: destroying will be triggered when the widget is destroyed. appendedtodom will be triggered when the widget is appended in the DOM and it not anymore in a detached state. This is useful as example if you need to position elements using absolute positioning or start an animation. removedfromdom will be triggered if the widget is removed from the DOM, usually because the detach() method has been called. 1.10. Tools and Shortcuts 9