Top Banner
The Future of the Web ...and the one polyfill that makes it all possible today
31
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: The Future of the Web

The Future of theWeb

...andtheonepolyfillthatmakesitallpossibletoday

Page 2: The Future of the Web

Browser Support

WeakMap:Chrome,FF,IE11

Object.observe:Chrome36

MutationObserver:allbutIE10,Safari6.1,Android4.3

WebComponents:Chrome36,FF32,Android4.4

Page 3: The Future of the Web

WeakMap

keysareobjects

valuescanbeanything

doesnotpreventgarbagecollectionofkeys

Page 4: The Future of the Web

Use Cases

Associatedatawithanelementw/outaugmentingtheelement

Associatedatawitharead-onlyobject

Moreefficientinformationhiding

Page 5: The Future of the Web

e.g.

varmap=newWeakMap();

varsomeEl=document.getElementById('someEl');

map.set(someEl,{foo:'bar'});

if(map.has(document.getElementById('someEl'){map.get(someEl).foo;//bar}

Page 6: The Future of the Web

Object.observe

Receivecallbackswhenanobjectchangesinanyway

NativeimplforAngular'sscope.$watch

ForArrays:Array.observe

Angularwilldelegatein2.0

Page 7: The Future of the Web

Angular

scope.name='Ray';

scope.$watch('name',function(newV,oldV){console.log('oldname:'+oldV);console.log('newname:'+newV);});

Page 8: The Future of the Web

Native

scope.name="Ray";

Object.observe(scope,function(change){if(change.name==='name'){console.log('oldname:'+change.oldValue);

console.log('newvalue:'+change.object[change.name]);}});

Page 9: The Future of the Web

MutationObserver

Receivecallbackswhenanelementchangesinanyway

Elementdescendendantscanalsobeobserved

ReplacementforinefficientMutationEvents

Page 10: The Future of the Web

e.g.varcallback=function(records){varrecord=records[0];//fordemosimplicityonlyvaradded=record.addedNodes;

if(added.length){varnewTags=0;for(vari=0;i<added.length;i++){if(added[i].className.indexOf('tag')>=0){newTags++;}}

if(newTags){alert(newTags+"newtagsadded.");}}};

varobserver=newMutationObserver(callback);observer.observe(tagsContainerEl,{childList:true});

Page 11: The Future of the Web

Web Components

Templates

CustomElements

ShadowDOM

Page 12: The Future of the Web

Templates

Create re-usable content

<template> is not rendered, must be

Any elements can be children

"activated"

<template><div>hithere</div><div>youcan'tseemerightnow</div><blink>luckilyforyou</blink></template>

Live example [jsbin]

Page 13: The Future of the Web

HTML Imports

PullanotherHTMLfileintothecurrentdocument

Injectapackagedcomponent(andalldeps)w/oneline

Re-usetemplatesstoredinacentrallocation

<linkrel="import"href="/license.html">

Page 14: The Future of the Web

Using an imported document

//Grabthe<link>thatimportsthelicensingblurbvarlicensingLink=document.getElementById('licenseLink');

//Grabtherelevantcontentfromtheimporteddoc

varcontent=licensingLink.import.getElementById('license');

//Appendthecontenttoourdocdocument.body.appendChild(content);

NOTE:ServermustreturnproperCORSheadersforcross-domainimports

Completeexample[jsbin]

Page 15: The Future of the Web

Custom Elements

MUST contain a dashTake heed angular directive writers.

Use ember? You're cool.

Why?To avoid conflict w/ future standardized elements.

<my-custom-element></my-custom-element>

Page 16: The Future of the Web

Creating Custom Elements

Initiallyan:unresolvedHTMLElement

To"resolve",youmustdocument.registerElement

Twotypes:1. New*CE2. TypeExtensionCE

*Ijustmadeupthisname

Page 17: The Future of the Web

Standard Custom Element

<foo-bar><!--unresolvedcontent--></foo-bar>

<script>

</script>

Liveexample[jsbin]

Page 18: The Future of the Web

Type Extension CustomElement

<videois="frame-grabber"src="somevideo.mp4">

</video>

varframeGrabber=document.createElement('video','frame-grabber');

frameGrabber.prototype.grab=function(){//grabcurrentframe&dosomethingw/it}

Page 19: The Future of the Web

Shadow DOM

Allowscreationofself-contained,sandboxedwebcomponents

AsingleNodecannowbemadeupof3differentsubtrees:

LightDOMShadowDOMComposedDOM

Page 20: The Future of the Web

Light DOM Tree

Partofthe"logicalDOM"

Contentofthecustomelementprovidedbyintegrator

<blink-is-back>Bringbacktheblinktag!</blink-is-back>

Displayedpre-shadow-rootrender

Page 21: The Future of the Web

Shadow DOM Tree

Partofthe"logicalDOM"Internaltothecustomelement,notachild

NotdirectlyaccessibleviathelightDOM

1. Includeelementonpage2. Register(ifCE)3. myElement.createShadowRoot();4. Appendcontenttoshadowroot5. Liveexample[jsbin]

Page 22: The Future of the Web

Composed DOM Tree

Contentthatisactuallyrendered

PiecesofthelightandshadowDOMcombined

ComposedDOMfrom CE:the<blink-is-back>

<blink-is-back><divstyle="text-decoration:line-through;">Bringbacktheblinkelement!</div><div>Butseriously,don'tmaketextblink.</div></blink-is-back>

Page 23: The Future of the Web

Polymer

Shimforallspecsdiscussedinthispresentation

Deferstonativeimpls,whenpresent

So,Youcanuseallofthesecoolspecsnow!

Page 24: The Future of the Web

The first devolving framework

LessimportantasWebComponentsetal.coverageincreases

Shoulddisappearwhenthesespecsareimplementedeverywhere

Page 25: The Future of the Web

A polyfill for Shadow DOM? How?

It'scomplicated

Everythingiswrapped

Requiredto:maintainencapsulationmaintainseparationbetweenlight&shadowDOMelementsretargeteventsoriginallydestinedfortheshadowDOMtree

Page 26: The Future of the Web

file-input

WebComponentbuiltusingPolymer

Allows<inputtype='file'>tobeeasilystyled

Built-infilevalidation

Moreintuitiveaccessto(valid)files

Page 27: The Future of the Web

e.g.

<file-inputclass="btnbtn-primary"extensions='["jpeg","jpg"]'minSize="500000"maxSize="3000000">

<spanclass="glyphiconglyphicon-file"></span>

Selectafile

</file-input>

Page 28: The Future of the Web

ajax-form

WebComponentbuiltusingPolymer

Submitaformwithoutapagereload

Sendcustomheadersw/formsubmit

Easyaccesstoserverresponse

Dirtsimplevalidationsupport/control

Submit<file-input>elementstoo!

Page 29: The Future of the Web

e.g.<formis="ajax-form"method="POST"action="form/handler"headers='{"X-Cust-Header":"FooBar"}'>

<inputtype="text"name="fullname"required>

<inputtype="submit">

</form>

form.addEventListener('submitted',function(e){if(event.detail.status>299){//submitmayhavefailed}});

form.addEventListener('invalid',function(e){e.detail.forEach(function(badEl){//dosomethingw/invalidelement});});

Page 30: The Future of the Web
Page 31: The Future of the Web

Useful Links

WeakMapInformationhidingwithWeakMapsObject.observeMutationObserverWebComponentscustomelementsPolymer<formis="ajax-form"><file-input>