yui on the go

68
YUI on the go How to include YUI components on demand Christian Heilmann Yahoo! Frontend Engineering Summit UK December 2007

Upload: christian-heilmann

Post on 05-Dec-2014

15.592 views

Category:

Technology


1 download

DESCRIPTION

My talk at the Yahoo! Frontend Engineering Summit in December 2007. It explains how you can embed the YUI component by component on demand rather than in one big chunk.

TRANSCRIPT

Page 1: YUI on the go

YUI on the goHow to include YUI components on demand

Christian HeilmannYahoo! Frontend Engineering Summit UK

December 2007

Page 2: YUI on the go

Why use libraries?

Page 3: YUI on the go

Use goodlibrary

Plan Code

Job done

Page 4: YUI on the go

Without libraries:

Page 5: YUI on the go

Browser HellPlan Code

OS inconsistenciesForget about obscure bugs

Cannot reproduce

Page 6: YUI on the go
Page 7: YUI on the go

> CVS commit> Conflict: > Too much fail error.

Page 8: YUI on the go

You.sacked = true

Page 9: YUI on the go

MCSE

Page 10: YUI on the go
Page 11: YUI on the go
Page 12: YUI on the go

“You want fries with that?”

Page 13: YUI on the go
Page 14: YUI on the go

Ergo:Libraries are good, mkay?

Page 15: YUI on the go

A common complaint.

Page 16: YUI on the go

“YUI is too big! If I want to build something on top of it, I need to include a lot of large

files!”

Page 17: YUI on the go

yahoo.js – 4.6kbdom.js – 35kbevent.js – 61kb

reset.css – 0.5kbfonts.css – 0.6kbgrids.css – 3kb

6 HTTP requests – 104.7kb

Page 18: YUI on the go

First Aid remedies:

– Use the minified YUI versions

Page 19: YUI on the go

yahoo-min.js – 1kbdom-min.js – 10.2kbevent-min.js – 13kb

reset-min.css – 0.4kbfonts-min.css – 0.2kbgrids-min.css – 2.4kb

6 HTTP requests – 27.2kb

Page 20: YUI on the go

First Aid remedies:

– Use the minified YUI versions – Cut down on included YUI

components by using the combined component includes.

Page 21: YUI on the go

yahoo-dom-event.js – 24.1kbreset-fonts-grids.css – 3.1kb

2 HTTP requests – 27.2kb

Page 22: YUI on the go

First Aid remedies:

– Use the minified YUI versions – Cut down on included YUI

components by using the combined component includes.

– Use the hosted YUI versions.

Page 23: YUI on the go

This is all packing and level crunching.

Page 24: YUI on the go

What we really want is...

Page 25: YUI on the go

On-demand delivery

Page 26: YUI on the go

Why is that needed?

Page 27: YUI on the go

Use cases:

Page 28: YUI on the go

Use cases:

– We don’t want to make users load things they don’t need.

Page 29: YUI on the go

Use cases:

– We don’t want to make users load things they don’t need.

– Distribution (badges, banners) works a lot better if it is one <script> and not 3.

Page 30: YUI on the go

Use cases:

– We don’t want to make users load things they don’t need.

– Distribution (badges, banners) works a lot better if it is one <script> and not 3.

– We can offer implementers to trigger extra functionality with markup:

Page 31: YUI on the go

Use cases:

– We don’t want to make users load things they don’t need.

– Distribution (badges, banners) works a lot better if it is one <script> and not 3.

– We can offer implementers to trigger extra functionality with markup:

“add an ‘animated’ CSS class for smooth transitions”

Page 32: YUI on the go

Example?

Page 33: YUI on the go

<div class="flickrbadge"> <p><a

href="http://www.flickr.com/photos/11414938%40N00">

My latest photos on flickr</a>

</p> </div> <script src="flickrbadgeloader.js"></script>

http://icant.co.uk/sandbox/flickrbadgev2/

Page 34: YUI on the go

<div class="flickrbadge"> <p><a

href="http://www.flickr.com/photos/11414938%40N00">

My latest photos on flickr</a>

</p> </div> <script src="flickrbadgeloader.js"></script>

http://icant.co.uk/sandbox/flickrbadgev2/

Page 35: YUI on the go

<div class="flickrbadge"> <p><a

href="http://www.flickr.com/photos/11414938%40N00">

My latest photos on flickr</a>

</p> </div> <script src="flickrbadgeloader.js"></script>

http://icant.co.uk/sandbox/flickrbadgev2/

Page 36: YUI on the go

How?

Page 37: YUI on the go

Supercool Solution:

The YUI Loader

http://developer.yahoo.com/yui/yuiloader

Page 38: YUI on the go

YUI Loader:

Page 39: YUI on the go

YUI Loader:

– Knows about dependencies

Page 40: YUI on the go

YUI Loader:

– Knows about dependencies – Automatically gets the newest

hosted versions

Page 41: YUI on the go

YUI Loader:

– Knows about dependencies – Automatically gets the newest

hosted versions– Works without yahoo.js

Page 42: YUI on the go

YUI Loader:

– Knows about dependencies – Automatically gets the newest

hosted versions– Works without yahoo.js– Extendable with non-YUI

components(!)

Page 43: YUI on the go

<script src="http://yui.yahooapis.com/2.3.1/build/yuiloader/yuiloader-beta-min.js"></script>

<script>loader = new YAHOO.util.YUILoader();loader.require('calendar','tabview');loader.insert(function() {

// Your code});</script>

Page 44: YUI on the go

http://yuiblog.com/assets/pdf/cheatsheets/yuiloader.pdf

Page 45: YUI on the go

Other option – 11kb is too much!

Page 46: YUI on the go

Rolling your own

The YAHOO_config way

Page 47: YUI on the go

YUI has an often forgotten configuration setting in

YAHOO_config.

Page 48: YUI on the go

YUI has an often forgotten configuration setting in

YAHOO_config.

http://developer.yahoo.com/yui/yahoo/#config

Page 49: YUI on the go

YUI has an often forgotten configuration setting in

YAHOO_config.

http://developer.yahoo.com/yui/yahoo/#configNow with more AWESOME!

Page 50: YUI on the go

It allows you to define a listener function that gets notified every time a YUI

component is loaded.

Page 51: YUI on the go

<script>function snitch(component){

console.log('can has YUI ' + component.name);

};YAHOO_config = {

listener:snitch};</script><script src="http://yui.yahooapis.com/2.3.1/build/yahoo/yahoo-min.js"></script><script src="http://yui.yahooapis.com/2.3.1/build/event/event-min.js"></script>

Page 52: YUI on the go

Using this in conjunction with your scripts and repeat calls

of the “snitch” function allows you to dynamically

include the YUI.

Page 53: YUI on the go

var myScript = function(){// other code function addScript(url){

var s = document.createElement('script');s.setAttribute('type', 'text/javascript');s.setAttribute('src', url);document.getElementsByTagName('head')[0].appendChild(s);

};function YUIready(o){

if(typeof o === 'undefined'){var APIurl = 'http://yui.yahooapis.com/2.3.1/' +

'build/yahoo-dom-event/' + 'yahoo-dom-event.js';

addScript(APIurl);} else {

if(o.name === 'yahoo-dom-event'){ // YUI is ready}

};};YUIready();return { YUIready:YUIready };

}();YAHOO_config = { listener:myScript.YUIready };

Page 54: YUI on the go

var myScript = function(){// other code function addScript(url){

var s = document.createElement('script');s.setAttribute('type', 'text/javascript');s.setAttribute('src', url);document.getElementsByTagName('head')[0].appendChild(s);

};function YUIready(o){

if(typeof o === 'undefined'){var APIurl = 'http://yui.yahooapis.com/2.3.1/' +

'build/yahoo-dom-event/' + 'yahoo-dom-event.js';

addScript(APIurl);} else {

if(o.name === 'yahoo-dom-event'){ // YUI is ready}

};};YUIready();return { YUIready:YUIready };

}();YAHOO_config = { listener:myScript.YUIready };

Page 55: YUI on the go

var myScript = function(){// other code function addScript(url){

var s = document.createElement('script');s.setAttribute('type', 'text/javascript');s.setAttribute('src', url);document.getElementsByTagName('head')[0].appendChild(s);

};function YUIready(o){

if(typeof o === 'undefined'){var APIurl = 'http://yui.yahooapis.com/2.3.1/' +

'build/yahoo-dom-event/' + 'yahoo-dom-event.js';

addScript(APIurl);} else {

if(o.name === 'yahoo-dom-event'){ // YUI is ready}

};};YUIready();return { YUIready:YUIready };

}();YAHOO_config = { listener:myScript.YUIready };

Page 56: YUI on the go

var myScript = function(){// other code

+ function addScript(url){};function YUIready(o){

if(typeof o === 'undefined'){var APIurl = 'http://yui.yahooapis.com/2.3.1/' +

'build/yahoo-dom-event/' + 'yahoo-dom-event.js';

addScript(APIurl);} else {if(o.name === 'yahoo-dom-event'){ // YUI is ready

}};

};YUIready();return { YUIready:YUIready };

}();YAHOO_config = { listener:myScript.YUIready };

Page 57: YUI on the go

var myScript = function(){// other code

+ function addScript(url){};function YUIready(o){

if(typeof o === 'undefined'){var APIurl = 'http://yui.yahooapis.com/2.3.1/' +

'build/yahoo-dom-event/' + 'yahoo-dom-event.js';

addScript(APIurl);} else {if(o.name === 'yahoo-dom-event'){ // YUI is ready

}};

};YUIready();return { YUIready:YUIready };

}();YAHOO_config = { listener:myScript.YUIready };

Page 58: YUI on the go

var myScript = function(){// other code

+ function addScript(url){};function YUIready(o){

if(typeof o === 'undefined'){var APIurl = 'http://yui.yahooapis.com/2.3.1/' +

'build/yahoo-dom-event/' + 'yahoo-dom-event.js';

addScript(APIurl);} else {if(o.name === 'yahoo-dom-event'){ // YUI is ready

}};

};YUIready();return { YUIready:YUIready };

}();YAHOO_config = { listener:myScript.YUIready };

Page 59: YUI on the go

var myScript = function(){// other code

+ function addScript(url){};function YUIready(o){

if(typeof o === 'undefined'){var APIurl = 'http://yui.yahooapis.com/2.3.1/' +

'build/yahoo-dom-event/' + 'yahoo-dom-event.js';

addScript(APIurl);} else {if(o.name === 'yahoo-dom-event'){ // YUI is ready

}};

};YUIready();return { YUIready:YUIready };

}();YAHOO_config = { listener:myScript.YUIready };

Page 60: YUI on the go

var myScript = function(){// other code

+ function addScript(url){};function YUIready(o){

if(typeof o === 'undefined'){var APIurl = 'http://yui.yahooapis.com/2.3.1/' +

'build/yahoo-dom-event/' + 'yahoo-dom-event.js';

addScript(APIurl);} else {if(o.name === 'yahoo-dom-event'){ // YUI is ready

}};

};YUIready();return { YUIready:YUIready };

}();YAHOO_config = { listener:myScript.YUIready };

Page 61: YUI on the go

var myScript = function(){// other code

+ function addScript(url){};function YUIready(o){

if(typeof o === 'undefined'){var APIurl = 'http://yui.yahooapis.com/2.3.1/' +

'build/yahoo-dom-event/' + 'yahoo-dom-event.js';

addScript(APIurl);} else {if(o.name === 'yahoo-dom-event'){ // YUI is ready

}};

};YUIready();return { YUIready:YUIready };

}();YAHOO_config = { listener:myScript.YUIready };

Page 62: YUI on the go

var myScript = function(){// other code

+ function addScript(url){};function YUIready(o){

if(typeof o === 'undefined'){var APIurl = 'http://yui.yahooapis.com/2.3.1/' +

'build/yahoo-dom-event/' + 'yahoo-dom-event.js';

addScript(APIurl);} else {if(o.name === 'yahoo-dom-event'){ // YUI is ready

}};

};YUIready();return { YUIready:YUIready };

}();YAHOO_config = { listener:myScript.YUIready };

Page 63: YUI on the go

var myScript = function(){// other code

+ function addScript(url){};function YUIready(o){

if(typeof o === 'undefined'){var APIurl = 'http://yui.yahooapis.com/2.3.1/' +

'build/yahoo-dom-event/' + 'yahoo-dom-event.js';

addScript(APIurl);} else {if(o.name === 'yahoo-dom-event'){ // YUI is ready

}};

};YUIready();return { YUIready:YUIready };

}();YAHOO_config = { listener:myScript.YUIready };

Page 64: YUI on the go

You can extend that to load different YUI components one

after the other.

Page 65: YUI on the go

var myScript = function(){// other code

+ function addScript(url){};function YUIready(o){

if(typeof o === 'undefined'){var APIurl = 'http://yui.yahooapis.com/2.3.1/' +

'build/yahoo-dom-event/' + 'yahoo-dom-event.js';

addScript(APIurl);} else {if(o.name === 'yahoo-dom-event'){ var url = 'http://yui.yahooapis.com/2.3.1/' +

'build/animation/' + 'animation-min.js';

addScript(url);}

};};YUIready();return { YUIready:YUIready };

}();YAHOO_config = { listener:myScript.YUIready };

Page 66: YUI on the go

var myScript = function(){// other code

+ function addScript(url){};function YUIready(o){

+ if(typeof o === 'undefined'){} else {if(o.name === 'yahoo-dom-event'){ var url = 'http://yui.yahooapis.com/2.3.1/' +

'build/animation/' + 'animation-min.js';

addScript(url);};if(o.name === 'animation'){

// done …};

};};YUIready();return { YUIready:YUIready };

}();YAHOO_config = { listener:myScript.YUIready };

Page 67: YUI on the go

You can also make it dependent on classes or IDs

used in the document, or configuration options of your main script and many more

things.

Page 68: YUI on the go

Have fun!

Thanks!