vanilla js*

34
Vanilla JS* Théodore 'nod_' Biadala JS Maintainer for Drupal core DrupalCon, Amsterdam 2014

Upload: theodore-biadala

Post on 22-Jun-2015

426 views

Category:

Technology


11 download

DESCRIPTION

Browsers APIs grew up, developers need to acknowledge it and make use of them — when appropriate. If we don't, browsers will stop caring and we're all going to have a bad time. This session will review common Drupal JS patterns found in core, contrib and projects and how they can be simplified (for some definition of simplified), pointing out frequent library abuse on the way. The topic of micro libraries will come up and I'll bring up the topic of hybrid approaches for all of us lazy programmers. We'll talk about where and how to draw the line for using a particular solution (this is a cue for the IE8 talk). Drupal 8 code will be taken as an example. The key concerns throughout the session are performance and maintainability. Unlike fanatics, I want you to be able to use what you learned knowing all the pros and cons. When you decide it's the right thing for you to use, you can trust your choice is the right one in your situation. The future is now! (for some definition of now)

TRANSCRIPT

Page 1: Vanilla JS*

Vanilla JS*

Théodore 'nod_' BiadalaJS Maintainer for Drupal core

DrupalCon, Amsterdam 2014

Page 2: Vanilla JS*

Vanilla JS*

Context

Page 3: Vanilla JS*

D8 Vanilla JS

active-link.jsannounce.js

drupal.js

Page 4: Vanilla JS*

jQueryAddicted to sugar

Page 5: Vanilla JS*

Duty to the web

Browsers improved

Use it or lose it

Help make the web better

Page 6: Vanilla JS*

Drawing the lineDrawing the line

Browser support

Code cleaninessTeam knowledge

Performance–Maintainability–Time

Page 7: Vanilla JS*

D8 Polyfills

classList

matchMedia

pictureFill

Page 8: Vanilla JS*

*Falling short

Event handling still sucks

AJAX handling too

Page 9: Vanilla JS*

Micro librariesMicro libraries

Page 10: Vanilla JS*

D8 micro libraries

domready

Page 11: Vanilla JS*

Micro libraries

One featureA little bit of sugar

Smallest size possible

Page 12: Vanilla JS*

Micro libraries

Events — bean, microevent, zoe… Ajax — Reqwest, http.js, ajax…

Many more at microjs.com

Page 13: Vanilla JS*

Problems

DuplicationBugs are spread outInconsistent quality

Page 14: Vanilla JS*

Hybrid approach

Page 15: Vanilla JS*

Size of micro libraries with quality of jQuery

Page 16: Vanilla JS*

Hybrid approach

core

Event

Sizzle

AJAX

Page 17: Vanilla JS*

Custom Builds !(kb, minified + gziped)

All — 29.1Sizzle — 8.6 Core — 2.2 Event — 6.9 Ajax — 7.0

Manipulation — 6.8 Traversing — 4.1

Page 18: Vanilla JS*

Custom Builds !(kb, minified + gziped)

All — 29.1Sizzle — 8.6 Core — 2.2 Event — 6.9 Ajax — 7.0

Manipulation — 6.8 Traversing — 4.1

Page 19: Vanilla JS*

Custom Builds !(kb, minified + gziped)

All — 29.1Sizzle — 8.6 Core — 2.2 Event — 6.9 Ajax — 7.0

Event+Ajax — 10.4 Manipulation — 6.8

Traversing — 4.1

Page 20: Vanilla JS*

Static builds

Page 21: Vanilla JS*

“franken-jQuery”#1541860-62

Page 22: Vanilla JS*

Vanilla JS*

Examples

Page 23: Vanilla JS*

ES5kangax.github.io/compat-table/es5/

Page 24: Vanilla JS*

Array

[1, 2, 3, 4].forEach(doSomething);[1, 2, 3, 4].map(doSomething);[1, 2, 3, 4].filter(doSomething);

Page 25: Vanilla JS*

DOM + Array

var lis = document.querySelectorAll('li') ;Array.prototype.forEach.call(lis, doSomething) ;

Page 26: Vanilla JS*

Object

Object.keys({ index1 : 1, index2 : 2}) .forEach(doSomething) ;

Page 27: Vanilla JS*

Function

doSomething.bind(thisObject) ;

Page 28: Vanilla JS*

querySelector(All)

document.querySelectorAll('.active') ;document.querySelector('#edit-username') ;

Page 29: Vanilla JS*

classList

div.classList.remove('active');div.classList.add('active');

div.classList.toggle('element-hidden');

div.classList.contains('active');

Page 30: Vanilla JS*

DOM DOM DOM DOM

p1.insertAdjacentHTML(position, html) ;

<!-- beforebegin --><p><!-- afterbegin -->Foo<!-- beforeend --></p><!-- afterend -->

Page 31: Vanilla JS*
Page 32: Vanilla JS*

Mobile-first is Vanilla-first

Help make « Franken-jQuery »

Page 33: Vanilla JS*

Discussion

Got code ?Where do you draw the line ?

What can Drupal do to help you ?

Page 34: Vanilla JS*

Thanks !

Théodore BIADALA

@nod_

[email protected]