<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" 
      xmlns:thr="http://purl.org/syndication/thread/1.0">
  <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/06/the-yahoo-user-interface-libra-1.html" />
  <link rel="self" type="application/atom+xml" href="http://www.insideria.com/atom.xml" />
  <id>tag:www.insideria.com,2009://34/tag:www.insideria.com,2008://34.23516-</id>
  <updated>2009-11-02T17:31:26Z</updated>
  <title>Comments for The YAHOO! User Interface Library - YUI (http://www.insideria.com/2008/06/the-yahoo-user-interface-libra-1.html)</title>
  <generator uri="http://www.sixapart.com/movabletype/">Movable Type 4.21-en</generator>
  <entry>
    <id>tag:www.insideria.com,2008://34.23516</id>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/06/the-yahoo-user-interface-libra-1.html" />
    <link rel="service.edit" type="application/atom+xml" href="http://blogs.oreilly.com/cgi-bin/mt/mt-atom.cgi/weblog/blog_id=34/entry_id=23516" title="The YAHOO! User Interface Library - YUI" />
    <published>2008-06-17T15:00:00Z</published>
    <updated>2008-06-17T23:49:14Z</updated>
    <title>The YAHOO! User Interface Library - YUI</title>
    <summary>Little more than a two years ago YAHOO! decided to make its library of cross-browser JavaScript components available to the public with a BSD license as the YUI, the YAHOO! User Interface library. It is now reaching the end of its second major version with dozens of minor versions, each new one bringing a few more components with it.</summary>
    <author>
      <name>Daniel Barreiro</name>
      
    </author>
    
    <category term="Features" />
    
    <content type="html" xml:lang="en" xml:base="http://www.insideria.com/">
      <![CDATA[<p>Little more than a two years ago YAHOO! decided to make its library of cross-browser JavaScript
components available to the public with a BSD license as the
YUI, the YAHOO! User Interface library.  It is now reaching the end of its
second major version with dozens of minor versions, each new one bringing a few more components with
it.</p>
<p>Since YAHOO! is the major user of YUI, actually, since the millions of users
accessing YAHOO! are the major end-users, the YUI is a very effective
tool (admittedly, not all YAHOO! uses the YUI yet). YUI was developed to satisfy an existing need in a tough and varied
environment.  Clients may be anywhere, with different bandwidths, can have
different browsers with different settings and several applications might need
to coexist in the same page.  </p>
<p>The most unusual feature of YUI as a JavaScript library is, strange
as it may sound, that it can work even if the end user does not have JavaScript
enabled.  YUI can be used to enhance existing markup, that is, a page can
be produced by the server as a plain HTML page, functional but
featureless.  If JavaScript is enabled, the YUI can enhance it with all the
modern features it provides.  For example, the Rich Text Editor component
expects an HTML textarea as its container.  If JavaScript is not enabled,
the user will still be able to edit plain text in the textarea. Menus and
TreeViews can take an unordered list of links so that if JavaScript is not
enabled, the page will be functional.  This feature, though important to
those who need to deliver services to the general public, has a cost, pages have
to be designer to look good with both JavaScript enabled and without; since the
initial markup has to be created dynamically pages cannot be cached on the
client side and the server side has to be able to reply to both asynchronous and
regular requests.</p>
<p>The YUI library also provides several CSS stylesheets.  Some of them are
meant to normalize the presentation across all browsers.  For example, the
default font sizes in different browsers are not exactly alike and these styles
makes all sizes compatible.  Another stylesheet provides several static
layouts based on absolute positioning and floats (the library also has a dynamic layout component). 
Furthermore, several components have their own stylesheets controlling
their visual presentation.  YUI allows for a skinning mechanism and though YUI
initially has provided only a single skin (the 'sam' skin named after its
designer), some users have made their own and at
least one has made <a target="_blank" href="http://yui.simplifiedcomplexity.org/index-sc.html">his public</a>.  </p>
<p>The YUI library has been carefully designed to coexist safely with other
third party code running on the same client such as mashups, ad providers,
search boxes and
such.  The whole library uses a single name in the global namespace, <code>YAHOO</code>,
thus avoiding collisions with variables declared elsewhere. HTML elements are
the other point of contact in between libraries coexisting on a page. 
Here, the YUI library uses the <code>yui-</code> prefix for any identifier attached to
an element, be it for the <code>id</code> attribute, <code>classNames</code> or extra custom
attributes.  Other libraries can
be used at the same time as long as they don't use the <code>YAHOO</code>
 variable or the <code>yui-</code> prefix.  Under this single variable name, several properties contain the several objects that make the
library.  All visible UI elements fall under <code>YAHOO.widget</code>: <code>YAHOO.widget.Editor</code>,
<code>YAHOO.widget.TreeView</code> and so on. Under <code>YAHOO.lang</code> we find those
functions that provide functionality that JavaScript should have but is missing,
deficient or unreliable, such as variable type detection.  Finally, <code>YAHOO.util</code>
gathers all the rest such as DOM manipulation, event handling, asynchronous
communication and others.  </p>
<p>This subdivision of the <code>YAHOO</code> namespace should not be confused with
'components'.  In YUI 'components' are basically delivery units.  A
single component might contain objects from more than one branch of the named
above.  The <code>Menu</code> component, for example, contains several objects
under <code>YAHOO.widget</code> for their visible elements but it also has some under <code>YAHOO.util</code>,
which are not visible but remain in the background keeping track of open menu
panels.</p>
<p>The performance enhancements in the library have been continuous. The YUI team receives
feedback from other departments within YAHOO!, some of them dedicated to
<a target="_blank" href="http://developer.yahoo.com/performance/">performance</a>, which then get reflected on the library. An important tool has been
the <a hre="http://developer.yahoo.com/yui/compressor/">YUI Compressor</a> which is able to reduce the size of the JavaScript and CSS
stylesheets to improve delivery time. For the smaller components, where the
latency time is greater than the transmission time, several such components were
packed into a single file which, once compressed, can be delivered in the
shortest possible time.  These are called aggregate files. The YUI team has even re-written several of the
components to maximize the compression rates, for example, avoiding references
to long global names, which cannot be minimized, using local variables instead.  And if you are concerned that your servers might not be capable
of efficiently delivering those components worldwide, YAHOO! has placed the
library in its own distributed servers for <a target="_blank" href="http://developer.yahoo.com/yui/articles/hosting/#background">
public access</a> so your servers only need to
provide your application and YAHOO! delivers the YUI library itself.</p>
<p>YUI achieves an unusual balance.  Though programmers might find other
libraries more elegant or cleaner, YUI satisfies most of those involved in
delivering enhanced applications to end users. It is tested in all major
browsers, it has a small size/performance ratio, it has been carefully designed to coexist
with other code on the client machine, it is
flexible in the communication options with the servers so that even existing
applications might be enhanced by YUI without requiring major reworking on the
server side and it can even allow you to deliver workable pages to browsers with
JavaScript disabled. </p>
<p>The YUI library is very well documented.  Besides the <a target="_blank" href="http://developer.yahoo.com/yui/docs/">API reference</a>,
produced automatically from the original source code, the <a target="_blank" href="http://developer.yahoo.com/yui/">user guides</a> and the
<a target="_blank" href="http://developer.yahoo.com/yui/examples/">examples</a> cover most of the features.  
The <a target="_blank" href="http://yuiblog.com/">YUI blog</a> and  <a target="_blank" href="http://developer.yahoo.com/yui/theater/">YUI Theater</a>
provides some more in-depth articles, 
pointers to related material elsewhere, news and videos.</p>
<p>The next major version will bring a reordering of the library since its own
unexpected growth has introduced a few inconsistencies. This will certainly
satisfy the programmers who want a cleaner library.   It wasn't until
some common patterns were detected in several of the components that some base
objects were developed and though now all new components inherit from those, the
old ones still have their own ways.  More exciting will be the possibility
of third party participation in code development.  So far, the YUI team is
the only one working on the library, with external developers able to
participate through <a target="_blank" href="http://sourceforge.net/tracker/?group_id=165715&atid=836476">SourceForge</a> in reporting bugs and suggesting future features
and the <a target="_blank" href="http://tech.groups.yahoo.com/group/ydn-javascript/">mailing list</a>.  With the reordering of the library and the
establishment of clearer guidelines, third parties will be able to effectively
develop new components or add to existing ones.</p>
<h2>Loading components</h2>
<p>There are two ways to load the YUI components, you can do it directly via <code>&lt;script&gt;</code>
and <code>&lt;link&gt;</code> tags, as shown in most <a target="_blank" href="http://developer.yahoo.com/yui/examples/">examples</a> 
or in <a href="http://www.insideria.com/2008/05/writing-your-first-yui-applica.html">Eric Miraglia's article</a> or you could use the <a target="_blank" href="http://developer.yahoo.com/yui/yuiloader/">YUI Loader</a>.  The Loader
is often overlooked in the documentation. The 'Getting Started' section of each component usually lists the library
dependencies providing a code block which you can copy and paste right into your
code. Most
people miss to read the brief sentence that gives the name you need to provide
the YUI Loader to load the component and all its dependencies.  Admittedly,
using the YUI Loader is slower than including the library files directly since
none of the files will even start to load until after the Loader itself has
loaded and decided what to do.  If performance is an issue, then it is
better to load components directly. To help you handle dependencies,
specially when there is more than one component involved, the 
<a target="_blank" href="http://developer.yahoo.com/yui/articles/hosting/#configure">configurator</a>
utility lets you build your own includes, otherwise, the YUI Loader is the
easiest choice. The configurator actually uses the YUI Loader to build the dependency lists.</p>
<p>Assuming that in your HTML page you have included the script for the YUI
Loader:</p>
<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
&lt;script src="<span class="quote">http://yui.yahooapis.com/2.5.1/build/yuiloader/yuiloader-beta-min.js</span>"
<span class="category2">type</span>="<span class="quote">text/javascript</span>"&gt;&lt;/script&gt;</pre>
</code>

</div></div> 
<p>Then you just need to put this code in the accompanying JavaScript</p>
<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
(<span class="category1">function</span>(){
     <span class="category1">var</span> loader = <span class="category1">new</span> YAHOO.util.YUILoader();
     
     loader.require('<span class="quote">editor</span>','<span class="quote">datatable</span>');
 
     loader.insert({
          <span class="linecomment">// filter: 'raw',  // or filter:'debug',</span>
          <span class="linecomment">// base: '/yui-2.5.1/build/',</span>
          loadOptional: <span class="category1">true</span>,
          onSuccess: <span class="category1">function</span>(){ 
               YAHOO.util.Event.onDOMReady(<span class="category1">function</span> () {
    
                    <span class="linecomment">// your code here</span>
    
                });
           },
          onFailure:<span class="category1">function</span>(msg) {
               YAHOO.<span class="category2">log</span>(msg,'<span class="quote">error</span>','<span class="quote">myPage</span>');
           }
      });
})();</pre>
</code>

</div></div> 
<p>The above sample carry lots of information, lets analyze it.  In the <code>&lt;script&gt;</code>
tag you can see the address for the YAHOO! servers which can be used to load the
YUI library.  The servers have copies of all the public releases of the
library since unavoidable differences in between versions might make one version
slightly incompatible with previous ones.  By having a folder for each
version, you can stick with the version you know your application works with
instead of rushing to change your application when the YUI version
changes.  The filename for the YUI Loader component also tells us that it
is still beta and we are loading the compressed version ('minified', thus the <code>-min</code>
suffix). </p>

<p>The JavaScript code that follows is enclosed in an anonymous function. 
Its purpose is to turn all the variables declared inside, such as <code>loader</code>,
local to that function and, thus, not visible in the global scope.  Notice
the very last set of empty parenthesis at the bottom of the code box indicating that the function is immediately
executed.  Within that anonymous function we declare a variable <code>loader</code>
which we initialize with an instance of the YUI Loader.  This is the first
YUI component we are using. As we mentioned, all YUI components
are defined under the <code>YAHOO</code> namespace.  
If, just as we did, we define all our own variables as local to
an anonymous function, we are further protected from the risk of namespace
collisions. </p>

<p>We then declare which components we want to load. There is no need to get concerned
with dependencies and such, the loader will take care of that.  We are
asking here for the DataTable and the Rich Text Editor components.  This
will also load any stylesheets associated with these components and if within
the dependency files it finds any group that belong to an aggregate file, it
will load that one, unless you explicitly tell it not to by setting the <code> allowRollup</code>
option to false.   </p>

<p>Finally we tell the loader to insert the requested components into our
page.  The <code>insert</code> method takes an object literal as its argument
specifying several options.  The main one is the <code> onSuccess</code> property which
will be called when  all scripts and stylesheets requested have been loaded and
are ready to use.  However, this does not mean that the page itself is ready to
use. Though in many cases the body of the page will be loaded by the time the
loader finishes its work, there might be cases when it is not, for example, when
the page contains information from many database queries which might delay its
completion.  So, we make a further check to see if the DOM is ready for us
to use via method <code> onDOMReady</code> which we call passing it a function which it
should call when everything is ready.  That is the place for our code, everything
we could need should be ready.  At this point we
are three levels deep into functions contained within functions so any
variables we declare inside are local to that function and not visible
elsewhere (variables used but not explicitly declared will go into the global namespace, 
a source of much frustration when they collide with others with the same name).</p>

<p>The loader has several options such as <code>filter</code> which lets you specify
which version of the files to load.  By default, the compressed versions get
loaded but you can specify <code>'raw'</code> to load the regular version which still
has all the formatting, comments and original variable names so it can be
stepped through with a debugger and make sense of it, or the <code>'debug'</code>
version which has extra code to log messages into the <a target="_blank" href="http://developer.yahoo.com/yui/logger/">YUI Logger</a>, another
component of the library which is also available to developers to log their own
messages in a consistent way in all supported browsers. </p>

<p>The <code>base</code> option allows you to change the default base path for all library
components.  By default the components get loaded from the YUI servers, if
you don't want that, you can say so.  The <code>loadOptional</code> option lets
you specify whether you want some extra non-vital dependencies that provide a
layer of  icing such as animated movements or drag and drop.  Finally, the <code>onFailure</code>
function gets called if anything went wrong and it provides a message indicating
the cause. We are using the YUI Logger to show it.  The YUI Logger might be
loaded or not, the call to <code>YAHOO.log</code> won't fail if it hasn't.  In
this example, we have not loaded it, but it is still good to have those log
messages in your code just in case something does go wrong.  Though the
Logger can be loaded by including the name <code>'logger'</code> in the call to <code>loader.require</code>,
to log any messages produced by the Loader, it needs to be included separately, just as the
Loader itself. </p>

<h2>Loading your own Components </h2>

<p>We can use the YUI Loader to load our own code and libraries.  We do this by
using the <code>addModule</code> method: </p>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
loader.addModule({
     <span class="category2">name</span>: '<span class="quote">myModuleName</span>',
     <span class="category2">type</span>: '<span class="quote">js</span>',
     fullpath: '<span class="quote">js/myModule.js</span>',
     requires: ['<span class="quote">myLibrary</span>', '<span class="quote">datatable</span>', '<span class="quote">connect</span>']
});</pre>
</code>

</div></div> 

<p>We have to give each module a name which can later be used in require lists,
tell the loader whether it is a JavaScript or CSS file, the path to locate it
and its dependencies which can be any mix of YUI components and our own which
should be declared elsewhere with this same <code>addModule</code> method.</p>

<p>To let the loader know when a module has been loaded, we need to add a line
of code at the end of each. </p>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
YAHOO.register('<span class="quote">myModuleName</span>',entryPoint,{<span class="category2">version</span>:'<span class="quote">2.5.1</span>',build:'<span class="quote">0</span>'});</pre>
</code>

</div></div> 

<p>The first argument should be the same name given to the <code>addModule</code>
method.  If a module fails to register, the <code>Loader.insert</code> method
will remain waiting and will never call the <code>onSuccess</code> callback function. </p>

<p>While page code can, for the most part,  be contained within the local
scope of a function and thus avoid cluttering the global namespace, libraries
have to be public and expose some of its members. We may device and manage an
schema analogous to the <code>YAHOO</code> namespace or we may fit within by using
the <code>YAHOO.namespace</code> function: </p>

<div class="acode" style="overflow: auto; padding: 10px;" ><div style="overflow-x: visible;">
<code language="perl">
<pre>
YAHOO.namespace('<span class="quote">myOrganization</span>'); </pre>
</code>

</div></div> 

<p>This will create a <code>YAHOO.myOrganization</code> object which can be later
populated with your own library objects.  This call can be safely repeated
in each of your library modules since it does not clear any previous contents,
if there is any. </p>

<p>Amongst the several objects we will likely include in our libraries are our
own customized versions of YUI objects.  From SimpleDialog we might derive
confirmation or information boxes styled to our site, from Panel we can make
wait or 'in progress' boxes or we might tailor the Rich Text Editor by adding or
removing tools from its toolbar or adding a completely separate toolbar. </p>
<p>
Functions <code>YAHOO.lang.extend</code> and <code>YAHOO.lang.augment</code> are very
helpful in doing that. They are located in the <code>lang</code> branch since, in a
language which in principle allows inheritance, it is quite funny that there
should be no built in method to do it.  Both methods could and ideally
should be added as prototypes to the <code>Object</code> object but to avoid clashes
with other code, the YUI library never modifies the base libraries and it tries,
as much as possible, to be tolerant should it find them modified by others. </p>
<hr/>
<p>The YUI library is a solid and ever growing product.  It has a very demanding in-house user, 
which constitutes a huge user base on its own
though it is far from the <a target="_blank" href="http://developer.yahoo.com/yui/poweredby/">only one</a>.  The YUI team is very responsive, all its members are 
regular contributors to the mailing list, many of them providing helpful articles and 
examples in their own personal web sites. 
The product is well documented and, should all else fail, 
the (uncompressed) code itself is clear and well commented.
The community of developers is also quite big with several of its members quite knowledgeable in 
many of the components so there are few questions that remain unanswered for long.
</p>
<p>In later articles, we will try some of its components</p>]]>
      
    </content>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23516-comment:2018032</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23516" type="text/html" href="http://www.insideria.com/2008/06/the-yahoo-user-interface-libra-1.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/06/the-yahoo-user-interface-libra-1.html#comment-2018032" />
    <title>Comment from adolfo_isassi on 2008-06-19</title>
    <author>
        <name>adolfo_isassi</name>
        <uri>http://www.flickr.com/photos/adolfo_isassi/</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.flickr.com/photos/adolfo_isassi/">
        <![CDATA[<p>YUI was an eye opener for web developers.<br />
It was an example of the level of quality that a (javascript) framework can achieve when an entity the size of Yahoo puts its resources behind it.</p>

<p>In my perception as a web developer, YUI raised the bar and marked the beginning of ajax as an enterprise level framework to deliver web interfaces. Soon after YUI, Ext JS appeared on the map as -the- apex of ajax.</p>

<p>But, YUI still stands as the pattern to follow when good'ol unlimited backwards compatibility is a requirement.</p>]]>
    </content>
    <published>2008-06-19T13:23:19Z</published>
  </entry>

  <entry>
    <id>tag:www.insideria.com,2008://34.23516-comment:2070204</id>
    <thr:in-reply-to ref="tag:www.insideria.com,2008://34.23516" type="text/html" href="http://www.insideria.com/2008/06/the-yahoo-user-interface-libra-1.html"/>
    <link rel="alternate" type="text/html" href="http://www.insideria.com/2008/06/the-yahoo-user-interface-libra-1.html#comment-2070204" />
    <title>Comment from Ryan I on 2009-08-11</title>
    <author>
        <name>Ryan I</name>
        <uri>http://www.thebridalbell.com</uri>
    </author>
    <content type="html" xml:lang="en" xml:base="http://www.thebridalbell.com">
        <![CDATA[<p>I started using YUI very early on.  One of the reasons I choose to use it vs  using prototype was that it had so much good documentation.</p>

<p>The most exciting piece of YUI in the 2.x versions have been the focus on web performance.</p>]]>
    </content>
    <published>2009-08-12T04:32:00Z</published>
  </entry>

</feed
