so, you think you know widgets

40
@DanielEricLee SO, YOU THINK YOU KNOW WIDGETS Friday, September 16, 2011

Upload: danielericlee

Post on 15-Jan-2015

3.918 views

Category:

Technology


1 download

DESCRIPTION

'So, you think you know widgets' A talk by Dan Lee at DojoConf 2011 in Washington, DC.

TRANSCRIPT

Page 1: So, you think you know widgets

@DanielEricLee

SO,YOU THINK YOU KNOW WIDGETS

Friday, September 16, 2011

Page 2: So, you think you know widgets

DAN LEE

Friday, September 16, 2011

Page 3: So, you think you know widgets

JAVASCRIPT

Friday, September 16, 2011

Page 4: So, you think you know widgets

Friday, September 16, 2011

Page 5: So, you think you know widgets

Stage One: Doubt

Friday, September 16, 2011

Page 6: So, you think you know widgets

Stage Two: Hate

Friday, September 16, 2011

Page 7: So, you think you know widgets

Stage Three: Begrudging Appreciation

Friday, September 16, 2011

Page 8: So, you think you know widgets

Stage Four: Love

Friday, September 16, 2011

Page 9: So, you think you know widgets

FOURSTAGES

OFWIDGET

WIZARDRY

Friday, September 16, 2011

Page 10: So, you think you know widgets

Stage One: Wonder

I never knew JavaScript could be awesome.

--My friend Tom

Friday, September 16, 2011

Page 11: So, you think you know widgets

Stage Two: Kicking the Tires

With great power, comes...people screwing up.

Friday, September 16, 2011

Page 12: So, you think you know widgets

Alpha Mistake: Accidental Static Fields

dojo.declare("test.Widget", [ dijit._Widget, dijit._Templated ],{ templateString : dojo.cache("test", "templates/Widget.html"), objectProperty: { } // Well, there goes your afternoon.

});

Friday, September 16, 2011

Page 13: So, you think you know widgets

Avoiding Accidental Statics

dojo.declare("test.Widget", [ dijit._Widget, dijit._Templated ], { templateString : dojo.cache("test", "templates/Widget.html"), // Initialize to null objectProperty: null, // Instantiate in constructor constructor: function() { this.objectProperty = {}; }});

Friday, September 16, 2011

Page 14: So, you think you know widgets

Another Common Stumbling Point

• Resources and Modules

• The magic of dojo.require

• Register Module Path

• Custom modules when using Dojo from a CDN

Friday, September 16, 2011

Page 15: So, you think you know widgets

You guys already knew this stuff, but....

• Don’t forget, you are JavaScript Astronauts

• You were loading modules when these

kids were in diapers

• Anticipating where newcomers will struggle

is the most powerful tool of the educator

Friday, September 16, 2011

Page 16: So, you think you know widgets

Stage Three: Gem Mining

The Staples® of JavaScript toolkits.

“Dojo: Yeah, we’ve got that.”

Friday, September 16, 2011

Page 17: So, you think you know widgets

My favorite gem: Attribute Maps

Bind widget properties to widget DOM Nodes.

Update property, DOM node gets updated automatically.

Friday, September 16, 2011

Page 18: So, you think you know widgets

Widget Attribute Maps

<!-- In Widget Template HTML --><span data-dojo-attach-point="heroNode"></span>

// Specified as a property on your widget definitionattributeMap: { hero: { node: "heroNode", type: "innerHTML" }}

// Calling 'set' on hero updates the innerHTML of 'heroNode'myWidget.set("hero", "Larry Bird");

Friday, September 16, 2011

Page 19: So, you think you know widgets

Widget Attribute Maps

// Also possible to bind to a CSS class or HTML attributeattributeMap: { // Bind to a CSS class prop: { node: "someNode", type: "class" }, // Bind to an HTML attribute prop2: { node: "imageNode", type: "attribute", attribute: "src" }}

Friday, September 16, 2011

Page 20: So, you think you know widgets

More Complex Bindings: Custom Setters

// Whenever widget.set(‘funkiness’, value) is called,// this function is called _setFunkinessAttr : function(brandNewFunk) { // Do what needs to be done to update the widget // to the new level of funkiness }

Add function to your widget with naming convention:

_setPropertynameAttr

Friday, September 16, 2011

Page 21: So, you think you know widgets

One Gotcha...

attributeMap: dojo.delegate(dijit._Widget.prototype.attributeMap, { // Attribute Map definition })

_Widget has an attributeMap, don’t clobber it.

Friday, September 16, 2011

Page 22: So, you think you know widgets

Gotcha resolved in Dojo 1.7...

• attributeMap is deprecated, to be removed in Dojo 2.0.• All property/DOM bindings can be specified via _set

convention. To specify an attributeMap relationship, return

an object instead of a function. Nice.

_setHeroAttr: { node: "heroNode", type: "innerHTML" }

Friday, September 16, 2011

Page 23: So, you think you know widgets

Demo Time

Let’s see those Attribute Maps in action.

It’s me. Of course we’ll use a silly widget.

Friday, September 16, 2011

Page 24: So, you think you know widgets

Beware @phiggins

Friday, September 16, 2011

Page 25: So, you think you know widgets

dojo.declare(“poopin.phijit”)

Friday, September 16, 2011

Page 26: So, you think you know widgets

Stay vigilant at DojoConf caughtyapoopin.com

Friday, September 16, 2011

Page 27: So, you think you know widgets

Phijit code: Let’s see a quick demo

attributeMap: { threatLevel: { node: "threatLevelNode", type: "class" }},_setCounterAttr: function(value) { var threatCss = "low"; this.counterNode.innerHTML = value; if(value >= 3 && value <= 4) { threatCss = "elevated"; } else if (value >= 5) { threatCss = "severe"; } this.set('threatLevel', threatCss);}

Friday, September 16, 2011

Page 28: So, you think you know widgets

Stage Four: Craftsmanship

Friday, September 16, 2011

Page 29: So, you think you know widgets

Memory Leaks

Historically, not a big deal for web apps.

That time is over.

Friday, September 16, 2011

Page 30: So, you think you know widgets

Tony Gentilcore

Awesome blog post about using

Chrome’s DevTools Heap Profiler:

http://gent.ilcore.com/2011/08/finding-memory-leaks.html

Web Performance Expert at Google:

Friday, September 16, 2011

Page 31: So, you think you know widgets

Widget Specific Leak

• Widgets within Widgets can leak

• Calling destroyRecursive() on a widget generally

cleans up child widgets

• Child widgets defined declaratively in HTML

template get cleaned up

• Widgets new’d programmatically do not get cleaned up

Friday, September 16, 2011

Page 32: So, you think you know widgets

Example Leaky code

dojo.declare("poopin.PhijitTriumvirate")

<!-- Widget Template --><div> <div data-dojo-attach-point="phijits"></div></div>

postCreate : function() { for(var i = 0; i < 3; i++) { var p = new poopin.Phijit(); // Leaky Phijit dojo.place(p.domNode, this.phijits, "last"); }}

Friday, September 16, 2011

Page 33: So, you think you know widgets

Should I avoid doing this?

No! This is perfectly natural,

just make sure you clean up after yourself.

Friday, September 16, 2011

Page 34: So, you think you know widgets

Clean up - Two techniques

Quick and Dirty: • Append your programmatically created widgets to this._supportingWidgets• Internal, subject to change.

Better: • Keep track of these widgets yourself, destroy them when

the parent is destroyed.• http://higginsforpresident.net/2010/01/widgets-within-widgets/

Friday, September 16, 2011

Page 35: So, you think you know widgets

Lightweight Leak Detection

dijit.registry to the rescue!

To detect leaks:• Query the registry

• Perform interactions that should result in net zero widgets

•Query the registry again to produce a diff

Friday, September 16, 2011

Page 36: So, you think you know widgets

dijit.registry Query code

// Query the registrymy_widgets = []; dijit.registry.forEach(function(widget){ my_widgets.push(widget.id);});console.log("Widgets Captured (" + dijit.registry.length + ") in total)");

// Query it again, produce a Diffvar leakyWidgets = [];dijit.registry.forEach(function(widget){ if(dojo.indexOf(my_widgets, widget.id) === -1) { leakyWidgets.push(widget.declaredClass); }});

console.log(leakyWidgets.length + " potential leaks found");console.log("Potential Leaks: " + leakyWidgets.join(", "));

Friday, September 16, 2011

Page 37: So, you think you know widgets

Bookmarklet Demo

Friday, September 16, 2011

Page 38: So, you think you know widgets

Most Important Stage

• Stage Two: Kicking the Tires

• This is where we lose people

• If you care about Dojo, you should not be OK with this

Friday, September 16, 2011

Page 39: So, you think you know widgets

This is so awesome

Friday, September 16, 2011

Page 40: So, you think you know widgets

THE END.@DanielEricLee

Friday, September 16, 2011