<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>danwebb.net - JavaScript</title>
  <id>tag:danwebb.net,2011:mephisto/javascript</id>
  <generator version="0.7.3" uri="http://mephistoblog.com">Mephisto Noh-Varr</generator>
  <link href="http://danwebb.net/feed/javascript/atom.xml" rel="self" type="application/atom+xml"/>
  <link href="http://danwebb.net/javascript" rel="alternate" type="text/html"/>
  <updated>2011-05-29T04:40:43Z</updated>
  <entry xml:base="http://danwebb.net">
    <author>
      <name>dan</name>
    </author>
    <id>tag:danwebb.net,2011-05-28:7593</id>
    <published>2011-05-28T22:21:00Z</published>
    <updated>2011-05-29T04:40:43Z</updated>
    <category term="JavaScript"/>
    <category term="hashbangs"/>
    <category term="javascript"/>
    <category term="pushstate"/>
    <category term="urls"/>
    <link href="http://danwebb.net/2011/5/28/it-is-about-the-hashbangs" rel="alternate" type="text/html"/>
    <title>It's About The Hashbangs</title>
<content type="html">
            &lt;p&gt;Before I get started here&#8217;s the disclaimer: The opinions expressed in this rant are my own personal opinions on web development and do not represent the views of my employer, its engineering organisation or any other employees.&lt;/p&gt;


	&lt;p&gt;A few months back there was a &lt;a href=&quot;http://isolani.co.uk/blog/javascript/BreakingTheWebWithHashBangs&quot;&gt;flurry&lt;/a&gt; of &lt;a href=&quot;http://intertwingly.net/blog/2011/02/09/Breaking-the-Web-with-hash-bangs&quot;&gt;blog&lt;/a&gt; &lt;a href=&quot;http://www.adequatelygood.com/2011/2/Thoughts-on-the-Hashbang&quot;&gt;posts&lt;/a&gt; and conversations over Twitter both for and against the now fairly common practice of using hashbang urls (&lt;a href=&quot;http://2011.jsconf.us/#!/schedule&quot;&gt;example&lt;/a&gt;) and JavaScript routing in favor of traditional URLs and full page loads.  There is also growing interest around &lt;a href=&quot;http://sammyjs.org/&quot;&gt;several&lt;/a&gt; &lt;a href=&quot;http://documentcloud.github.com/backbone/&quot;&gt;JavaScript&lt;/a&gt; MVC frameworks that are make heavy use of this technique. Since people started doing this kind of thing I&#8217;ve been pretty squeamish about the idea.  At the time that this discussion erupted across the web I really wanted to comment on it but until recently, although I was almost certain that hashbang URLs were destructive, I found myself unable to put in definite terms why.&lt;/p&gt;


	&lt;p&gt;As you probably know if you&#8217;ve been reading this blog for a while, I have for a long time been an avid proponent of progressive enhancement and as many people correctly pointed out many of the arguments against hashbang URLs seemed to fold this philosophy in which clouded the issue quite a lot.  In a &lt;a href=&quot;http://www.adequatelygood.com/2011/2/Thoughts-on-the-Hashbang&quot;&gt;well reasoned post&lt;/a&gt;, my colleague, Ben Cherry pointed this out and expressed that it wasn&#8217;t really hashbangs that were the problem and that they were merely a temporary work around until we get pushState support.  As he put it, &#8220;It&#8217;s Not About The Hashbang&#8221;.&lt;/p&gt;


	&lt;p&gt;After quite a lot of thought and some attention to some of the issues that surround web apps that use hashbang URLs I&#8217;ve come to conclusion that it most definitely &lt;em&gt;is&lt;/em&gt; about the hashbangs.  This technique, on its own, is destructive to the web.  The implementation is inappropriate, even as a temporary measure or as a downgrade experience.&lt;/p&gt;


	&lt;p&gt;Let me explain.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;URLs are important&lt;/strong&gt;. The reason the web is so powerful is that it is a web of information.  Any piece of content can reference any other piece of content directly. Our information is no longer siloed into various disconnected libraries, now all our data is linked together.  The web is much better at doing this than as a platform for delivering applications but yeah, that&#8217;s a whole other blog post. The means by which one piece of data is linked with another piece of data is via a &lt;span class=&quot;caps&quot;&gt;URL&lt;/span&gt;.  That makes the &lt;span class=&quot;caps&quot;&gt;URL&lt;/span&gt; possibly the most important part of the web. If you are working on a web app I assume you value its content.  If you value the content that a web app holds then you need to value it&#8217;s URLs even more. Directly addressable content is what makes web apps better than desktop apps.  It&#8217;s certainly not the UIs.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;URLs are forever&lt;/strong&gt;. The web has a pretty long memory.  Techniques and technology may change but content published to the web gets indexed, archived and otherwise preserved as do the URLs that they link to.  There&#8217;s no such thing as a temporary fix when it comes to URLs.  If you introduce a change to your &lt;span class=&quot;caps&quot;&gt;URL&lt;/span&gt; scheme you are stuck with it for the forseeable future.  You may internally change your links to fit your new &lt;span class=&quot;caps&quot;&gt;URL&lt;/span&gt; scheme but you have no control over the rest of the web that links to your content.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Cool URLs don&#8217;t change&lt;/strong&gt;. For this and other reasons, Tim Berners-Lee wrote a classic article, &lt;a href=&quot;http://www.w3.org/Provider/Style/URI.html&quot;&gt;Cool URLs don&#8217;t change&lt;/a&gt; in which he explains how to make future proof URLs and why that is important.  If you change your URLs you sever links with from the rest of the web.  You&#8217;ve just turned your web app into a data silo.  Your content has just become a lot less useful.  However, as much as we try it&#8217;s pretty impossible not to introduce change from time to time, sometimes data does need to be deleted, sometimes you need to move to a new domain name, sometimes you just need to reorganise.&lt;/p&gt;


	&lt;p&gt;Luckily, &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt; gives us the tools to handle this gracefully.  If content is deleted we can tell the web it&#8217;s no longer there with a 410 (thanks Nick!), if it&#8217;s moved to a different place on the web we can tell the world its new location with a 301 or a 302.  &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt; gives us the ability to manage change.  Further to that, it&#8217;s years old, fairly well specified and most importantly understood by not just browsers but all devices that can access the web including search engines and other spiders.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Going under the radar&lt;/strong&gt;. So, you&#8217;ve implemented hashbang URLs. This means that the part of the &lt;span class=&quot;caps&quot;&gt;URL&lt;/span&gt; after the #, the identifies the specific content, is not even sent in the &lt;span class=&quot;caps&quot;&gt;HTTP&lt;/span&gt; request.  It&#8217;s completely invisible to your server.  As far as your server is concerned it&#8217;s receiving requests for the root document and sending it with a 200 success code no matter what.  It no longer has the ability to determine if the &lt;span class=&quot;caps&quot;&gt;URL&lt;/span&gt; has moved to a different location or even if the content being requested exists at all.  This entire job is left up to some JavaScript that happens to be running on that page.  Sure, your javascript can examine the hash portion of the &lt;span class=&quot;caps&quot;&gt;URL&lt;/span&gt;, show the relevant content or if it&#8217;s missing show a &#8216;Content not found&#8217; message.  It can even redirect to different locations internal and external to the web app.&lt;/p&gt;


	&lt;p&gt;The important difference is that this is entirely opaque to anything that hasn&#8217;t got a JavaScript runtime and a document object model.  Spiders and search indexers can and do sometimes implement JavaScript runtimes.  However, even in this case there&#8217;s no well recognised way to say &#8216;this is a redirect&#8217; or &#8216;this content is not found&#8217; in a way that non-humans will understand.  You&#8217;ve just rendered your content invisible to everything apart from people running certain browsers. The hashbang itself is an &lt;a href=&quot;http://code.google.com/web/ajaxcrawling/docs/getting-started.html&quot;&gt;attempt to address this&lt;/a&gt; by Google but it&#8217;s quite a painful thing to implement and why get yourself into a situation where you are creating a fix for something you just broke.  Just don&#8217;t break it in the first place.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Once you hashbang, you can&#8217;t go back&lt;/strong&gt;. This is probably the stickiest issue. Ben&#8217;s post put forward the point that when pushState is more widely adopted then we can leave hashbangs behind and return to traditional URLs.  Well, fact is, you can&#8217;t.  Earlier I stated that URLs are forever, they get indexed and archived and generally kept around.  To add to that, cool URLs don&#8217;t change.  We don&#8217;t want to disconnect ourselves from all the valuable links to our content.  If you&#8217;ve implemented hashbang URLs at any point then want to change them without breaking links the &lt;em&gt;only&lt;/em&gt; way you can do it is by running some JavaScript on the root document of your domain. Forever.  It&#8217;s in no way temporary, you are stuck with it.&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;It&#8217;s not all doom and gloom&lt;/strong&gt;.  For the web apps that have made the jump already it&#8217;s too late but I urge you to think really hard about making the jump to hashbang URLs when creating new content or considering a switch from traditional URLs.  There is a path forward in the not too distant future.  pushState is coming to browsers at quite a rate and, as &lt;a href=&quot;http://warpspire.com/&quot;&gt;Kyle Neath&lt;/a&gt; said to me in a bar last week, is probably the most important innovation in web development since Firebug.  You can implement, as &lt;a href=&quot;https://github.com/blog/760-the-tree-slider&quot;&gt;Github have done&lt;/a&gt;, pushState for browsers that support it but by all means fall back to traditional URLs &lt;em&gt;rather than&lt;/em&gt; hashbang URLs.  Even if some users are getting hashbang URLs they will be publishing content linking to them, tweeting them and bookmarking them and you&#8217;ll be stuck with supporting them all the same.&lt;/p&gt;


	&lt;p&gt;Can we all agree to let it go the way of flash intros, please?&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://danwebb.net">
    <author>
      <name>dan</name>
    </author>
    <id>tag:danwebb.net,2011-02-10:7578</id>
    <published>2011-02-10T05:51:00Z</published>
    <updated>2011-02-10T05:53:35Z</updated>
    <category term="JavaScript"/>
    <category term="Rails"/>
    <category term="design"/>
    <category term="javascript"/>
    <category term="metaprogramming"/>
    <category term="programming"/>
    <link href="http://danwebb.net/2011/2/10/writing-a-new-library-sketch-it-out-first" rel="alternate" type="text/html"/>
    <title>Writing a new library? Sketch it out first</title>
<content type="html">
            &lt;p&gt;So you&#8217;re embarking on writing a new library. You might dive right in to the code and work straight on solving the problem or you might take a more considered approach and start thinking about how to model the problem, what classes and methods you need to create, how they interact and so on.  However, rather than doing that why not start with sketching out not how the library works but how you would like it to be used.  I think you&#8217;ll see pretty positive results if you try it.&lt;/p&gt;


	&lt;p&gt;Usability isn&#8217;t just for designers.  If you want your co-workers or other developers to use and be productive with this code your creating its got to be as simple and fun to use as your user interfaces.  Your library&#8217;s &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; &lt;em&gt;is&lt;/em&gt; a user interface.  History has shown that libraries and open source projects that put their &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; design up front are far more successful than their competitors.  Take jQuery for instance, you can bet that when &lt;a href=&quot;http://ejohn.org&quot;&gt;John&lt;/a&gt; sat down to create jQuery he wasn&#8217;t thinking &#8220;How do I animate &lt;span class=&quot;caps&quot;&gt;CSS&lt;/span&gt; style?&#8221; or &#8220;How do I create and event system?&#8221; his main concern was how he wanted jQuery to be used.  At least initially, the internals of jQuery were a mish mash of various libraries that came before it.  What actually powered the explose growth of jQuery was the revolutionary &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; concepts it introduced: Method chaining, Selector-focussed &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;, simple plug in interface and so on.  Other success stories like Ruby on Rails shared a similar focus on &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; design.&lt;/p&gt;


	&lt;p&gt;So, how do you write your code in such a way that it will make programmers happy and propell you into internet stardom? When you start out writing a new library or feature, start with a sketch.  Cast aside any ideas that you might have already have about the implementation details and try to also cast aside any technical constraints and just start writing how you&#8217;d like your code to be used.  Let&#8217;s take a &lt;span class=&quot;caps&quot;&gt;DOM&lt;/span&gt; builder, for example.  Mashing strings (or indeed using the terribly designed &lt;span class=&quot;caps&quot;&gt;W3C DOM API&lt;/span&gt;) is no fun in JavaScript.  If you were taking an implementation first approach you might start with an &lt;span class=&quot;caps&quot;&gt;JSON&lt;/span&gt;-style data structure describing your &lt;span class=&quot;caps&quot;&gt;DOM&lt;/span&gt; fragment or maybe even consider a set of objects that map to the various types of elements and this may well be how you implement it under the hood but put that aside for now. How would your code look when using the library?  Here&#8217;s my first pass:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;var fragment = build(
  div({ id: 'contact' },
    ul(
      li('Email: whatever@thing.com'),
      li('Twitter: ', a({ href: 'http://twitter.com/danwrong' }, '@danwrong')) 
    ) 
  )
);&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Come up with the simplest, most aesthetically pleasing &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; you can and keep itterating on it until you&#8217;ve weeded out all the complexity you can.  If certain arguments are optional or have a sensible defaults then make sure that the developer doesn&#8217;t have to worry about them unless they need to, is your library similar to any other library?  If it is maybe its worth adopting the conventions of that library so it acts in a way that other developers expect it to.  Developers dislike like reading documentation as much as your site&#8217;s users dislike read help pages.  Strive for an &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; so simple that you can describe it in a few sentences.&lt;/p&gt;


	&lt;p&gt;Once you are happy with your sketch start building it out. You might need a little meta programming magic to realise the UI you are going for and that&#8217;s fine but use with caution.  You need to weigh up whether the magic you are adding is going to make your code act in a way that is unpredictable to developers used to working with the language in question or if it will prevent developers from using their existing knowledge of the language to solve problems with your code.  If you smell badness then leave the magic behind. In our example above we need to do quite a bit of work with the function arguments in order to support variable amounts of child nodes and to allow optional attribute hashes. In this case I&#8217;d make the call that this doesn&#8217;t add any extra confusion to the code so I&#8217;m happy to go with that.&lt;/p&gt;


	&lt;p&gt;Also, at this point consider how the &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; you are trying to design will impact real world pratical issues.  For instance, in the example above it I would need to create functions for all &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; tags on the global namespace.  Am I happy with my library a defining a global function called a()?  No. Time to scale back on that idea a little.&lt;/p&gt;


	&lt;p&gt;Finally, when it comes to the implementation ensure that the architecture is sound and don&#8217;t try to couple the under the hood implementation with the &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; design to a degree that it&#8217;ll make the quality of the library suffer.  I often view the &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; as a simple, pretty veneer over the top of a well crafted solution.  Don&#8217;t hide the raw internals, make them accessable alongside the simple &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt;.  That way if the developer is on the beaten track they get a simple &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; but if they have specific needs they can delve right in and get stuff done.&lt;/p&gt;


	&lt;p&gt;So yeah, next time you write some code.  Think about sketching out the &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; first rather than leaving it as an after thought.  Your colleagues will thank you.&lt;/p&gt;


	&lt;p&gt;I&#8217;d love to speak more about this at &lt;a href=&quot;http://2011.jsconf.us&quot;&gt;JSConf&lt;/a&gt;.  The competition is very hot but here&#8217;s &lt;a href=&quot;http://2011.jsconf.us/#/proposal/c7bf7fec8a0afceeabf584d143d16862&quot;&gt;my proposal&lt;/a&gt; if your interested. If it&#8217;s picked I&#8217;ll go in to detail on some great and poor examples of &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; design and also show off some nice techniques for bringing your &lt;span class=&quot;caps&quot;&gt;API&lt;/span&gt; sketches to life.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://danwebb.net">
    <author>
      <name>dan</name>
    </author>
    <id>tag:danwebb.net,2010-05-26:3501</id>
    <published>2010-05-26T06:37:00Z</published>
    <updated>2010-05-26T13:25:10Z</updated>
    <category term="JavaScript"/>
    <category term="Projects"/>
    <category term="Rails"/>
    <category term="experiments"/>
    <category term="javascript"/>
    <category term="oembed"/>
    <category term="rails"/>
    <category term="twaudio"/>
    <link href="http://danwebb.net/2010/5/26/added-oembed-and-embeddable-player-to-twaud-io" rel="alternate" type="text/html"/>
    <title>Added OEmbed and Embeddable Player to twaud.io</title>
<content type="html">
            &lt;p&gt;I&#8217;ve just added &lt;a href=&quot;http://oembed.org&quot;&gt;OEmbed&lt;/a&gt; to &lt;a href=&quot;http://twaud.io&quot;&gt;twaud.io&lt;/a&gt; for fun and profit.  I&#8217;d not really looked into it until &lt;a href=&quot;http://dustindiaz.com&quot;&gt;Dustin&lt;/a&gt; and &lt;a href=&quot;http://twitter.com/dsa&quot;&gt;Russ&lt;/a&gt; pointed out and I really like the idea although it seems a bit under done at the moment (You can only have &#8216;image&#8217;, &#8216;video&#8217; or &#8216;rich&#8217; as media types.  What about audio?).  I&#8217;ve been meaning to implement an embeddable player for twaud.io for a long time so I decided to add them in.&lt;/p&gt;


	&lt;p&gt;The OEmbed endpoint is:&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;http://twaud.io/oembed.{format}&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;It supports &lt;span class=&quot;caps&quot;&gt;JSON&lt;/span&gt;, XML and &lt;span class=&quot;caps&quot;&gt;JSON&lt;/span&gt;-P (if you provide a callback parameter to a json request).  For example:&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;http://twaud.io/oembed.json?url=http%3A%2F%2Ftwaud.io%2FYf&#38;callback=myCallback&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;Returns this:&lt;/p&gt;


&lt;code&gt;&lt;pre&gt;myCallback({
  &quot;height&quot;:&quot;65&quot;,
  &quot;provider_url&quot;:&quot;http://twaud.io&quot;,
  &quot;title&quot;:&quot;Little mix of all the dubstep tunes I've been listening to lately: WRONG BEAT&quot;,
  &quot;type&quot;:&quot;rich&quot;,
  &quot;html&quot;:&quot;&amp;lt;iframe allowtransparency='true' frameborder='0' scrolling='no' src='http://twaud.io/embed/Yf' style='width: 395px;  height: 65px; border: none;'&amp;gt;&amp;lt;/iframe&amp;gt;\n&quot;,
  &quot;audio_type&quot;:&quot;audio/mpeg&quot;,
  &quot;audio_url&quot;:&quot;http://twaud.io/audio/Yf&quot;,
  &quot;width&quot;:&quot;395&quot;,
  &quot;author_name&quot;:&quot;danwrong&quot;,
  &quot;version&quot;:&quot;1.0&quot;,
  &quot;author_url&quot;:&quot;http://twitter.com/danwrong&quot;,
  &quot;provider_name&quot;:&quot;twaud.io&quot; 
});&lt;/pre&gt;&lt;/code&gt;

	&lt;p&gt;The &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; property contains a snippet of &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; that renders the player:&lt;/p&gt;


&lt;p&gt;&amp;lt;iframe src=&quot;http://twaud.io/embed/Yf&quot;&gt;&amp;lt;/iframe&gt;&lt;/p&gt;

	&lt;p&gt;All very beta at the moment but give it a try.  Also, check the short dubstep mix I did a while back.  Will post a longer one soon.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://danwebb.net">
    <author>
      <name>dan</name>
    </author>
    <id>tag:danwebb.net,2010-01-27:3441</id>
    <published>2010-01-27T14:55:00Z</published>
    <updated>2010-01-27T15:10:50Z</updated>
    <category term="JavaScript"/>
    <category term="html"/>
    <category term="html5"/>
    <category term="javascript"/>
    <link href="http://danwebb.net/2010/1/27/put-that-data-attribute-away-son-you-might-hurt-someone" rel="alternate" type="text/html"/>
    <title>Put that data-* attribute away, son...You might hurt someone</title>
<content type="html">
            &lt;p&gt;&lt;span class=&quot;caps&quot;&gt;HTML 5&lt;/span&gt; data-* attributes allow us to add &lt;a href=&quot;http://www.w3.org/TR/2009/WD-html5-20090825/dom.html#embedding-custom-non-visible-data&quot;&gt;custom attributes&lt;/a&gt; to elements as long as they are prefixed with &#8216;data-&#8217; and since this was first discussed on &lt;a href=&quot;http://ejohn.org/blog/html-5-data-attributes/&quot;&gt;John Resig&#8217;s blog&lt;/a&gt; I&#8217;ve been interested in how people will use and abuse this feature.  I greeted the feature with mixed feelings.  It&#8217;s definitely a simple way to enrich the semantic value of &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; pages as well as helping to improve some of the more &lt;a href=&quot;http://microformats.org/wiki/abbr-design-pattern&quot;&gt;toxic parts&lt;/a&gt; of Microformats.  &lt;span class=&quot;caps&quot;&gt;XML&lt;/span&gt; namespaces are definitely a more complete solution but this is a simple and immeadiately adoptable means to add invisible semantic data to &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; documents.&lt;/p&gt;


	&lt;p&gt;However, as John hinted in his post, there&#8217;s an enormous temptation for JavaScript authors to use this to embed configuration data for their scripts directly into &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt;.  Many developers have been itching for an excuse to do this for a long time.  Some just added &lt;a href=&quot;http://alex.dojotoolkit.org/2007/09/slides-from-my-standards-heresy-talk/&quot;&gt;attributes willy-nilly&lt;/a&gt; like crazy web standards bandits, some would love to do add arbiturary configuration into their &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; but felt a bit squeamish about moving away from the &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; specs and opted to abuse the class attribute from within the standard.  For the record, I&#8217;d tend to side with the former.  If it works and there&#8217;s a good reason for it then I say do it.  However, back then I explained &lt;a href=&quot;http://www.danwebb.net/2007/10/7/custom-attributes-and-class-names&quot;&gt;why there is no good reason to add unsemantic configuration data into your &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt;&lt;/a&gt; and now that we have standards-approved carte-blanche to do this I&#8217;d like to reiterate that it&#8217;s still not the way forward.  If you&#8217;ve not read that article then its worth a quick read before you go on.&lt;/p&gt;


	&lt;p&gt;By all means, use data-* attributes to add semantically valuable data to your &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; but if you are just using it to prop up a script you are writing &lt;strong&gt;think again&lt;/strong&gt;.&lt;/p&gt;


	&lt;h3&gt;An Example&lt;/h3&gt;


	&lt;p&gt;If you&#8217;ve not already watched it go now and watch &lt;a href=&quot;http://jquery14.com/day-11&quot;&gt;Yehuda&#8217;s Screencast on evented programming with jQuery&lt;/a&gt;.  The ideas in here represent a massive progression in client-side scripting.  It&#8217;s nothing short of essential viewing.  However, it also happens to be the latest example I&#8217;ve come across of needless use of data-* attributes and, while not wanting to take away from how progressive and clever the content is as a whole, I feel the need to use it as my counter-example for this article.&lt;/p&gt;


	&lt;p&gt;In the screencast, Yehuda is creating a tab interface.  The markup he proposes is something like this:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;html&quot;&gt;&amp;lt;ul class=&amp;quot;tabs&amp;quot;&amp;gt;
  &amp;lt;li data-content=&amp;quot;first&amp;quot;&amp;gt;First&amp;lt;/li&amp;gt;
  &amp;lt;li data-content=&amp;quot;second&amp;quot;&amp;gt;Second&amp;lt;/li&amp;gt;
  &amp;lt;li data-content=&amp;quot;third&amp;quot;&amp;gt;Third&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

&amp;lt;div class=&amp;quot;pane&amp;quot; id=&amp;quot;first&amp;quot;&amp;gt;Some content&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;pane&amp;quot; id=&amp;quot;second&amp;quot;&amp;gt;Some content&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;pane&amp;quot; id=&amp;quot;third&amp;quot; class=&amp;quot;selected&amp;quot;&amp;gt;Some content&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The idea being that when the tab &amp;lt;li&amp;gt; is clicked the script then interrogates data-content to decide which div to show.  However, without the JavaScript operating on this the &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; has no semantical value.  The &amp;lt;li&amp;gt;s are just list elements (and will be read as such by assistive technologies).  In fact, the browser doesn&#8217;t know that those list elements are in anyway associated with &amp;lt;div&amp;gt;s below.  Here&#8217;s how I think it should be marked up:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;html&quot;&gt;&amp;lt;ul class=&amp;quot;section-nav&amp;quot;&amp;gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#first&amp;quot;&amp;gt;First&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#second&amp;quot;&amp;gt;Second&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
  &amp;lt;li&amp;gt;&amp;lt;a href=&amp;quot;#third&amp;quot;&amp;gt;Third&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;

&amp;lt;div class=&amp;quot;section&amp;quot; id=&amp;quot;first&amp;quot;&amp;gt;Some content&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;section&amp;quot; id=&amp;quot;second&amp;quot;&amp;gt;Some content&amp;lt;/div&amp;gt;
&amp;lt;div class=&amp;quot;section&amp;quot; id=&amp;quot;third&amp;quot;&amp;gt;Some content&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Now, before we even add JavaScript we have links that we can click that will jump you to the specified content.  If you click the back button you will jump back to the previous tab&#8217;s content.  With this in place you could even make tabs work solely by using &lt;span class=&quot;caps&quot;&gt;CSS&lt;/span&gt; and the :target pseudo-selector.  If you wanted to go &lt;span class=&quot;caps&quot;&gt;HTML 5&lt;/span&gt; crazy you could even use &amp;lt;nav&amp;gt; and &amp;lt;section&amp;gt; elements which would further enhance the semantics of the document.  By correctly associating the tab link and the tab content we can take advantage of the browsers facilities to navigate this type of content even before we get out the old JavaScript crowbar.&lt;/p&gt;


	&lt;p&gt;With this markup as a base it&#8217;s then just as trivial to hook in the script but instead of interogatting data-content we just look at the anchor of the link.  Because we are now using anchors, users can deep link into a particular tab, it would be trivial to support the back button and assistive technologies will make better sense of it, amoung other things.&lt;/p&gt;


	&lt;h3&gt;Leave Yehuda Alone!&lt;/h3&gt;


	&lt;p&gt;Of course, I&#8217;m picking apart what was a very simple and purposefully contrived example, but as usage of data-* attributes picks up, it&#8217;s important to not abuse this facility and to continue find as many semantic hooks for your scripts as possible.  It may now be a &#8220;standard&#8221; but it doesn&#8217;t mean that its a good solution.  When looking for hooks for my scripts, this is the process I follow:&lt;/p&gt;


	&lt;p&gt;1. Build up your markup to be as meaningful as possible.  If it submits a request it should be a &amp;lt;form&amp;gt;, if its linking to another piece of content it&#8217;s an &amp;lt;a&amp;gt;.  Even if you&#8217;re building a very complex piece of UI seek to build as much of it as you can into your document (while keeping the semantics intact) before you go anywhere near your JavaScript.&lt;/p&gt;


	&lt;p&gt;2. Write your script to take advantage of the semantics your &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; document has to offer.  This will get you a long way in many cases, however, you may well find that there is still configuration information you need to pass into your script.  Rather than turn to data-* attributes its best to consider inferring this information via context in the same way that &lt;span class=&quot;caps&quot;&gt;CSS&lt;/span&gt; does.  This way you can assert things like &#8220;all &amp;lt;input&amp;gt;s with type &#8216;slider&#8217; and a class &#8216;day&#8217; have a min of 1 and a max of 31&#8221; then you can change this in one place rather than visiting each element&#8217;s data-* attributes individually.    &lt;a href=&quot;http://www.danwebb.net/2007/10/7/custom-attributes-and-class-names&quot;&gt;Read this article for more detail on how to do that&lt;/a&gt;. We don&#8217;t need to change the heading colour in every single heading element in our site now we have &lt;span class=&quot;caps&quot;&gt;CSS&lt;/span&gt;, let&#8217;s not start doing that kind of thing again now we have data-* attributes.&lt;/p&gt;


	&lt;p&gt;I welcome the data-* attribute.  It&#8217;s a simple and immediately useful method to add custom semantic data to &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; documents.  Just avoid using it to litter implementation-specific crap into your documents :)&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://danwebb.net">
    <author>
      <name>dan</name>
    </author>
    <id>tag:danwebb.net,2009-07-02:3373</id>
    <published>2009-07-02T11:22:00Z</published>
    <updated>2009-07-02T11:32:57Z</updated>
    <category term="JavaScript"/>
    <category term="Low Pro"/>
    <category term="firefox"/>
    <category term="fix"/>
    <category term="javascript"/>
    <category term="lowpro"/>
    <link href="http://danwebb.net/2009/7/2/low-pro-for-prototype-and-firefox-3-5" rel="alternate" type="text/html"/>
    <title>Low Pro for Prototype and Firefox 3.5</title>
<content type="html">
            &lt;p&gt;Just a quick note for those googling for a solution to this.  Older versions of Low Pro for Protoype will be experiencing problems with behaviors in Firefox 3.5.  This issue has been fixed so pick up the latest version &lt;a href=&quot;http://github.com/danwrong/low-pro/raw/2f47a651ab1143b2e067de7d2381f615c389a14e/dist/lowpro.js&quot;&gt;from GitHub&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;An interesting upshot of this is that I found out something pretty nice which is kind of obvious but had never occurred to me before.  If you create a function in a closure then that function can refer to itself because it has access to that closure&#8217;s variables.  I normally use arguments.callee to get a reference to a function from within its body but there&#8217;s never really any need to do that:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;(function() {
  var aFunction = function() {
    aFunction.thing = 47;
  };

  aFunction();

  alert(aFunction.thing); //=&amp;gt; 47;
})();&lt;/code&gt;&lt;/pre&gt;
          </content>  </entry>
  <entry xml:base="http://danwebb.net">
    <author>
      <name>dan</name>
    </author>
    <id>tag:danwebb.net,2008-06-23:3342</id>
    <published>2008-06-23T22:56:00Z</published>
    <updated>2008-06-24T00:24:12Z</updated>
    <category term="Events"/>
    <category term="JavaScript"/>
    <category term="Low Pro"/>
    <category term="Misc"/>
    <category term="Rails"/>
    <category term="boring"/>
    <category term="bullshit"/>
    <category term="chitchat"/>
    <category term="donotread"/>
    <category term="events"/>
    <category term="general"/>
    <category term="lowpro"/>
    <category term="prattle"/>
    <category term="rack"/>
    <category term="rails"/>
    <category term="reproduction"/>
    <category term="speaking"/>
    <category term="work"/>
    <link href="http://danwebb.net/2008/6/23/it-s-been-a-long-time" rel="alternate" type="text/html"/>
    <title>It's Been A Long Time...</title>
<content type="html">
            &lt;p&gt;But I&#8217;ve not just been sitting on my arse playing &lt;span class=&quot;caps&quot;&gt;GTA IV&lt;/span&gt;, oh no.  Well, not all the time anyway.  The reason I&#8217;ve not posted anything (or been particularly active on the web in general) is that I&#8217;ve been damn busy.  Most importantly, Catherine kindly gave birth to our first son, &lt;a href=&quot;http://flickr.com/photos/danwebb/2361976989/&quot;&gt;Max&lt;/a&gt;, back in March which has been quite a change and sapped a lot of my hacking time.  I have to say though, despite the horror stories that many veteran parents like to feed you, our experience has only been good.  In fact, not good, great.  I recommend this reproducing lark.&lt;/p&gt;


	&lt;p&gt;Secondly, I&#8217;ve been hacking away nearly full time on one of my favourite projects to date, &lt;a href=&quot;http://peoplesmusicstore.com&quot;&gt;Peoples Music Store&lt;/a&gt; with &lt;span class=&quot;caps&quot;&gt;LRUG&lt;/span&gt; stalwart and renowned anarchist, &lt;a href=&quot;http://abscond.org&quot;&gt;James &#8216;Bringing London To Its Very Knees&#8217; Darling&lt;/a&gt; which is maturing nicely under private beta as we speak.  Peoples Music Store is a great idea from some of the guys behind &lt;a href=&quot;http://bleep.com&quot;&gt;bleep.com&lt;/a&gt; whereby users can construct and customise their very own download store from the music they love then get free music themselves if people buy from their store.  It&#8217;s a great way to both promote and show off you&#8217;re own music taste or in depth genre knowledge and find new music from stores you trust while getting some free digital swag along the way.  I&#8217;m probably not explaining it well so just drop me a line if you want and invite and the site will explain itself.  Public launch is coming in a month or so.&lt;/p&gt;


	&lt;p&gt;Building Peoples Music Store has been a great learning experience.  We run the site on a cloud computing platform and from content ingestion to audio preview delivery to application servers to download packaging and delivery everything has been designed to scale horizontally &#8211; and I&#8217;m pretty proud of it.  Thin, Rack, Sphinx, God, Starling and a whole load more cool open source gear is all running in there. I really need to get to blogging some of what I&#8217;ve discovered about working with &lt;a href=&quot;http://rack.rubyforge.org/&quot;&gt;Rack&lt;/a&gt;.  It simply is the dog&#8217;s bollocks.&lt;/p&gt;


	&lt;p&gt;So, enough of the excuses.  What&#8217;s on the horizon?&lt;/p&gt;


	&lt;h3&gt;Speaking and Conferences&lt;/h3&gt;


	&lt;p&gt;I&#8217;ve taken some time of speaking and conferencing in general so as to spend lots of time with Catherine and Max but come September I&#8217;m restarting the conference trail.  Firstly, I&#8217;m doing a presentation and a tutorial (with &lt;a href=&quot;http://jlaine.net&quot;&gt;Jarkko Laine&lt;/a&gt;) at RailsConf Europe all about JavaScript related Rails stuff and I&#8217;m likely to have a slot at &lt;a href=&quot;http://vivabit.com/atmediaajax&quot;&gt;@media Ajax&lt;/a&gt; as well.  Also, I&#8217;ll be heading to &lt;a href=&quot;http://2008.dconstruct.org&quot;&gt;dConstruct&lt;/a&gt; as is the tradition.&lt;/p&gt;


	&lt;h3&gt;Hacking and Open Source Business&lt;/h3&gt;


	&lt;p&gt;Although I&#8217;ve not commited to Low Pro or Low Pro JQ for a good while now they are both very much alive.  I&#8217;ve simply not come across anything that I&#8217;ve felt the need to add for a while.  If you have any suggestions or patches do let me know.  I&#8217;ve actually got time to commit them at the moment.  Another little project that I&#8217;m hoping to get off the ground is called &lt;strong&gt;Evil&lt;/strong&gt; which is going to contain lots of Merb/Rack goodness.  The first by-product of which is the &lt;a href=&quot;http://github.com/danwrong/merb_openid&quot;&gt;merb_openid&lt;/a&gt; gem for consuming OpenID in Merb apps (it&#8217;s still not quite production ready though so don&#8217;t go using it just yet).  I&#8217;ll let you know what Evil actually does when (or if) I actually get something working.&lt;/p&gt;


	&lt;p&gt;So, that&#8217;s all for now.  Just a bit of a status report.  I promise I&#8217;ll get some useful content written that you actually care about very soon.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://danwebb.net">
    <author>
      <name>dan</name>
    </author>
    <id>tag:danwebb.net,2008-02-03:3002</id>
    <published>2008-02-03T16:00:00Z</published>
    <updated>2008-02-04T00:28:00Z</updated>
    <category term="JavaScript"/>
    <category term="Low Pro"/>
    <category term="javascript"/>
    <category term="jquery"/>
    <category term="lowpro"/>
    <link href="http://danwebb.net/2008/2/3/how-to-use-low-pro-for-jquery" rel="alternate" type="text/html"/>
    <title>How To Use Low Pro For jQuery</title>
<summary type="html">&lt;p&gt;It seems that my initial version of &lt;a href=&quot;http://github.com/danwrong/low-pro-for-jquery/tree/master/src/lowpro.jquery.js?raw=true&quot;&gt;Low Pro for jQuery&lt;/a&gt; has gotten enough interest to continue
with it but, as always, my previous post was extremely lacking in actual detail about how to use
the damn thing.  So without further ado here we go.  It&#8217;s pretty simple really and, as its a port
of a subset of Low Pro for Prototype, anything that goes for that also goes for jLow.&lt;/p&gt;


	&lt;p&gt;Heh, let&#8217;s not call it that again.  Bad.&lt;/p&gt;


	&lt;p&gt;Anyway, on with the show&#8230;&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;It seems that my initial version of &lt;a href=&quot;http://github.com/danwrong/low-pro-for-jquery/tree/master/src/lowpro.jquery.js?raw=true&quot;&gt;Low Pro for jQuery&lt;/a&gt; has gotten enough interest to continue
with it but, as always, my previous post was extremely lacking in actual detail about how to use
the damn thing.  So without further ado here we go.  It&#8217;s pretty simple really and, as its a port
of a subset of Low Pro for Prototype, anything that goes for that also goes for jLow.&lt;/p&gt;


	&lt;p&gt;Heh, let&#8217;s not call it that again.  Bad.&lt;/p&gt;


	&lt;p&gt;Anyway, on with the show&#8230;&lt;/p&gt;
&lt;h3&gt;Class-based OO&lt;/h3&gt;


	&lt;p&gt;In order for Low Pro behavior classes to work with jQuery we need a class implementation.  The
one I&#8217;ve based this on is the version from Prototype originally based on Alex Arnell&#8217;s work. This
is detailed in &lt;a href=&quot;http://prototypejs.org/learn/class-inheritance&quot;&gt;this article on the prototype site&lt;/a&gt;. The
only difference is that instead of using Class.create you use $.klass:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Draggable = $.klass({
  initialize: function(options) { },
  onmousedown: function() {}
});

GhostedDraggable = $.klass(Draggable, {
  onmousedown: function($super) {
    // do extra stuff here then call original method...
    $super();
  }
});&lt;/code&gt;&lt;/pre&gt;

	&lt;h3&gt;Attaching the behavior class to elements&lt;/h3&gt;


	&lt;p&gt;To attach the behavior class to some 
elements you can use the attach method:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;$('div.product').attach(GhostedDraggable, { anOption: thing });&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;code&gt;attach&lt;/code&gt; creates a new instance of the given behavior class for each element in the collection and
attaches them to these elements.  Any subsequent arguments are passed to the class&#8217;s &lt;code&gt;initialize&lt;/code&gt; function.  
A few magic things happen here.  First, &lt;code&gt;this.element&lt;/code&gt; in the 
behavior instance is set to a dollared version of the element it&#8217;s attached to.  Secondly, every 
method beginning with on (eg. onclick, onsubmit etc) gets bound as an event handler on the attached
element.  However, in the event handler functions themselves, &lt;code&gt;this&lt;/code&gt; points to the behavior instance
rather than the element so you can call it&#8217;s other methods.  You can obviously get to the element
by using &lt;code&gt;this.element&lt;/code&gt; if you need to though.&lt;/p&gt;

	&lt;h3&gt; And for the bonus round&#8230;&lt;/h3&gt;


	&lt;p&gt;So, that&#8217;s about all there is to it.  One added bonus is that if you are using &lt;a href=&quot;http://brandonaaron.net/docs/livequery/&quot;&gt;livequery&lt;/a&gt; then Low
Pro will automatically use it to attach any new behaviors after the &lt;span class=&quot;caps&quot;&gt;DOM&lt;/span&gt; is changed without you needing to worry about it.&lt;/p&gt;


	&lt;p&gt;Here&#8217;s a super simple example behavior that simply adds and removes a &lt;span class=&quot;caps&quot;&gt;CSS&lt;/span&gt; class name when it is
hovered over.  It&#8217;s about as bare bones as you can get while showing off most of the features:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Hover = $.klass({
  initialize: function(hoverClass) {
    this.hoverClass = hoverClass;
  },
  onmouseover: function() {
    this.element.addClass(this.hoverClass);
  },
  onmouseout: function() {
    this.element.removeClass(this.hoverClass);
  }
});

jQuery(function($) {
  $('span.name').attach(Hover, 'myClassName');
});&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;For more information about how you might use behavior classes take a look at &lt;a href=&quot;/2007/7/18/low-pro-behaviours-101-part-2&quot;&gt;this article.&lt;/a&gt; ANother good point of reference is the built-in Remote.Link and Remote.Form
behaviors that are built in to Low Pro.  Take a look at the &lt;a href=&quot;http://github.com/danwrong/low-pro-for-jquery/tree/master/src/lowpro.jquery.js?raw=true&quot;&gt;source&lt;/a&gt;
Also a Low Pro site is on its way shortly. As I use it more myself I&#8217;ll post more real examples but you can post any questions to the &lt;a href=&quot;http://groups.google.co.uk/group/low-pro&quot;&gt;Low Pro list&lt;/a&gt;.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://danwebb.net">
    <author>
      <name>dan</name>
    </author>
    <id>tag:danwebb.net,2008-01-31:2971</id>
    <published>2008-01-31T00:56:00Z</published>
    <updated>2008-01-31T14:43:25Z</updated>
    <category term="JavaScript"/>
    <category term="Low Pro"/>
    <category term="experiments"/>
    <category term="javascript"/>
    <category term="jquery"/>
    <category term="lowpro"/>
    <link href="http://danwebb.net/2008/1/31/low-pro-for-jquery" rel="alternate" type="text/html"/>
    <title>Low Pro For jQuery?</title>
<summary type="html">&lt;p&gt;A few days ago &lt;a href=&quot;http://groups.google.co.uk/group/low-pro/browse_thread/thread/29b96e32fb68f45e&quot;&gt;a discussion started about porting Low Pro over to jQuery&lt;/a&gt;.  It&#8217;s been on my mind 
for a while (especially since &lt;a href=&quot;http://meme-rocket.com/2007/09/07/agnostic-unobtrusive-javascript/&quot;&gt;Bill blogged about agnostic &lt;span class=&quot;caps&quot;&gt;UJS&lt;/span&gt;&lt;/a&gt;) but I&#8217;d hesitated in doing this for a few
reasons.  Firstly, of course, jQuery is the mortal enemy of Prototype and only one library shall
remain come The Quickening. Aside from this completely rational reason, I wondered whether jQuery
really needed anything like Low Pro at all.  After all, jQuery has always been designed with
&#8216;unobtrusiveness&#8217; in mind and was written with most of what Low Pro originally intended to fix 
in Prototype implemented from the start.  There was no need for something like Event.addBehavior 
because essentially that&#8217;s what jQuery is:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Event.addBehavior({
  'a.external:click': function() { //...code...// },
  'div.product:mouseover': function() { //...code...// }
});&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Can be achieved in jQuery like this:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;jQuery(function($) {
  $('a.external').click(function() { //...code...// });
  $('div.product').mouseover(function() { //...code...// });
});&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;And rather than ever going near inline event handlers this unobtrusive style was the de-facto
method of working for the jQuery crowd.  The angel on your scripting shoulder.  However, as you
can probably tell from reading this blog, I&#8217;ve never really adopted jQuery as my main library
and this was due to one big reason (and a bundle of small ones but I&#8217;ll leave them out)...&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;A few days ago &lt;a href=&quot;http://groups.google.co.uk/group/low-pro/browse_thread/thread/29b96e32fb68f45e&quot;&gt;a discussion started about porting Low Pro over to jQuery&lt;/a&gt;.  It&#8217;s been on my mind 
for a while (especially since &lt;a href=&quot;http://meme-rocket.com/2007/09/07/agnostic-unobtrusive-javascript/&quot;&gt;Bill blogged about agnostic &lt;span class=&quot;caps&quot;&gt;UJS&lt;/span&gt;&lt;/a&gt;) but I&#8217;d hesitated in doing this for a few
reasons.  Firstly, of course, jQuery is the mortal enemy of Prototype and only one library shall
remain come The Quickening. Aside from this completely rational reason, I wondered whether jQuery
really needed anything like Low Pro at all.  After all, jQuery has always been designed with
&#8216;unobtrusiveness&#8217; in mind and was written with most of what Low Pro originally intended to fix 
in Prototype implemented from the start.  There was no need for something like Event.addBehavior 
because essentially that&#8217;s what jQuery is:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Event.addBehavior({
  'a.external:click': function() { //...code...// },
  'div.product:mouseover': function() { //...code...// }
});&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Can be achieved in jQuery like this:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;jQuery(function($) {
  $('a.external').click(function() { //...code...// });
  $('div.product').mouseover(function() { //...code...// });
});&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;And rather than ever going near inline event handlers this unobtrusive style was the de-facto
method of working for the jQuery crowd.  The angel on your scripting shoulder.  However, as you
can probably tell from reading this blog, I&#8217;ve never really adopted jQuery as my main library
and this was due to one big reason (and a bundle of small ones but I&#8217;ll leave them out)...&lt;/p&gt;
&lt;p&gt;The one big reason was that, while jQuery was super simple and concise when working on smaller
projects, it offered no help in structuring larger applications.  All you get in jQuery, aside from
Ajax methods and a handful of utilities, is the ability to select nodes then doing something 
with them. On the other hand Prototype is much rounder in scope.  It generally plumps out JavaScript 
as a language adding lots of useful methods to built-ins, a host of functional programming tools 
and recently a full Class-based OO system with inheritance and the whole shebang which has formed
the back bone of Low Pro&#8217;s behavior classes.&lt;/p&gt;


	&lt;p&gt;Low Pro&#8217;s behavior classes have become my favourite solution to the problem of structuring complex
Ajax applications in a simple and maintainable way. Even hugely complex applications can be separated
up into a number of groups of elements with attached behaviors.  These behaviors maintain their
own state, respond to events and can also expose there own public methods.  They are linked to an
element but the element doesn&#8217;t know about them which eliminates the need for expandos on elements and
also most of the reasons for having element references in closures which is good for keeping memory usage
at bay.  For instance, a click on
a TabBar instance can call the loadContent method of a ContentPanel.  The result is more than a
set of widgets, its a way of splitting up huge complicated interfaces into a collection of small,
loosely joined parts each simple in its own right and totally responsible for its part of the interface.
This approach becomes incredibly useful when you introduce server-side requests and responses into
the mix but that&#8217;s a whole other article.&lt;/p&gt;


	&lt;p&gt;In jQuery land this kind of effect can be achieved via plugins.  You&#8217;ll find a ton of widgets
in the repository that have this kind of UI:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;$('#example4').draggable({ helper: 'clone', revert: true });&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The plugin architecture is one of jQuery&#8217;s many strong points but this kind of approach has a couple
of disadvantages.  Firstly, the plugin is created within a closure which means usually if you want to
change the behavior of the plugin you literally need to pop open the source and change it.  The second
and related disadvantage is that because of the difficulty in changing plugins they tend to have
a whole raft of options to allow you to configure how it works.  The problem with this is that
individual widgets can end up pretty monolithic.  Wouldn&#8217;t it be be better if you could just take
the basic draggable behavior and just augment it to your liking?&lt;/p&gt;


	&lt;p&gt;Well, in Low Pro behaviors are classes so we can do this by simply subclassing another behavior
and overriding or adding what we need.  For example, we can create GhostedDraggable as a subclass
of Draggable.  No need to hack any existing code at all.  As well as having the power and convenience
of jQuery&#8217;s &lt;span class=&quot;caps&quot;&gt;DOM&lt;/span&gt; manipulation we can have a simple and powerful way to structure larger, more
complex applications.  I can have my jQuery cake and eat it.&lt;/p&gt;


	&lt;p&gt;So, I&#8217;ve started experimenting with &lt;a href=&quot;http://github.com/danwrong/low-pro-for-jquery/tree/master/src/lowpro.jquery.js?raw=true&quot;&gt;Low Pro for jQuery.&lt;/a&gt;
Here&#8217;s a preview:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Hover = $.klass({
  initialize: function(hoverClass) {
    this.hoverClass = hoverClass;
  },
  onmouseover: function() {
    this.element.addClass(this.hoverClass);
  },
  onmouseout: function() {
    this.element.removeClass(this.hoverClass);
  }
});

$('div.products').attach(Hover);&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;If you want to leave feeedback or abuse &lt;a href=&quot;http://groups.google.co.uk/group/low-pro&quot;&gt;jump on the Low Pro list.&lt;/a&gt;
Do you think it has a place in the jQuery landscape?  How do you currently structure complex applications
in jQuery?&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;&lt;span class=&quot;caps&quot;&gt;UPDATE&lt;/span&gt;&lt;/strong&gt;: Thanks to Chris of &lt;a href=&quot;http://errtheblog.com&quot;&gt;Err The Blog&lt;/a&gt; Low Pro JQ is &lt;a href=&quot;http://github.com/danwrong/low-pro-for-jquery/tree/master/&quot;&gt;rocking on GitHub&lt;/a&gt;.  Fork away.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://danwebb.net">
    <author>
      <name>dan</name>
    </author>
    <id>tag:danwebb.net,2007-12-12:2558</id>
    <published>2007-12-12T00:23:00Z</published>
    <updated>2007-12-13T17:49:59Z</updated>
    <category term="JavaScript"/>
    <category term="Low Pro"/>
    <category term="javascript"/>
    <category term="lowpro"/>
    <category term="release"/>
    <link href="http://danwebb.net/2007/12/12/low-pro-0-5-now-compatible-with-prototype-1-6" rel="alternate" type="text/html"/>
    <title>Low Pro 0.5: Now Compatible With Prototype 1.6</title>
<content type="html">
            &lt;p&gt;Today I &lt;a href=&quot;http://svn.danwebb.net/external/lowpro/tags/rel_0.5/&quot;&gt;tagged Low Pro 0.5&lt;/a&gt; for release which now works with Prototype 1.6.  There are a number of things about this release that are worth mentioning aside from the compatibility.  Firstly, it&#8217;s gotten a little smaller as Prototype core now includes most of the functionality Low Pro used to add (DOM Ready support, inserting using &lt;span class=&quot;caps&quot;&gt;DOM&lt;/span&gt; nodes and a lot more).  It&#8217;s also got a couple of new features so here&#8217;s a rundown:&lt;/p&gt;


	&lt;ol&gt;
	&lt;li&gt;&lt;strong&gt;Event.onReady delegates to the new dom:loaded event&lt;/strong&gt;: Except that as before if functions are added after the &lt;span class=&quot;caps&quot;&gt;DOM&lt;/span&gt; is loaded they fire immediately.&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;DOMBuilder now delegates to Prototype&#8217;s new Element&lt;/strong&gt;: Now difference in usage here though, just less code.&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;Low Pro&#8217;s &lt;span class=&quot;caps&quot;&gt;DOM&lt;/span&gt; methods are now gone&lt;/strong&gt;: Prototype core does everything you should need now.&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;Behavior.create() works just like the new Class.create()&lt;/strong&gt;: Yes, you can now create behavior classes that inherit from other behaviors (or indeed any other class).  &lt;a href=&quot;http://prototypejs.org/learn/class-inheritance&quot;&gt;See the Prototype&#8217;s site for more information.&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;New core behaviors&lt;/strong&gt;: The Remote and Observed behaviors are now included in the core so you can now turn normal links and forms into Ajaxy links and forms even more easily.&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;Event.addBehavior.reassignAfterAjax is now false by default&lt;/strong&gt;:  Normally, if you are relying on this behavior it&#8217;s much more efficient to move to a solution using event delegation.  However, if you do want your behaviors reassigned to new content after Ajax calls then go ahead and set it back to true again.  Another solution is to manually call Event.addBehavior.reload();&lt;/li&gt;
	&lt;/ol&gt;


	&lt;p&gt;So that&#8217;s about it.  As you can see, it&#8217;s getting smaller as Prototype fills the gaps and graduating into more of a pure behavior framework.  I&#8217;d be interested in adding more core behaviors for other common tasks as well as possibly getting together some kind of behavior library.  I know I&#8217;m building up a fair few and I&#8217;d love to see what everyone else is doing (in fact I&#8217;ve already seen some great stuff) so suggestions are more than welcome&#8230;as are bug reports and patches.  For both of these and general assistance try &lt;a href=&quot;http://groups.google.co.uk/group/low-pro&quot;&gt;the Google Group&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;&lt;a href=&quot;http://svn.danwebb.net/external/lowpro/tags/rel_0.5/dist/lowpro.js&quot;&gt;Grab the new version&lt;/a&gt; and have a play.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://danwebb.net">
    <author>
      <name>dan</name>
    </author>
    <id>tag:danwebb.net,2007-11-22:2502</id>
    <published>2007-11-22T10:09:00Z</published>
    <updated>2007-11-22T10:23:05Z</updated>
    <category term="Events"/>
    <category term="JavaScript"/>
    <category term="atmedia"/>
    <category term="atmediaajax"/>
    <category term="event"/>
    <category term="javascript"/>
    <category term="lowpro"/>
    <category term="metaprogramming"/>
    <category term="presentation"/>
    <link href="http://danwebb.net/2007/11/22/media-ajax" rel="alternate" type="text/html"/>
    <title>@media Ajax</title>
<content type="html">
            &lt;p&gt;Well, that turned out rather nicely I must say.  Really high quality presentations and great attendees &#8211; I really hope that Patrick makes it happen again.  Here are the slides from my presentation:  Metaprogramming JavaScript.&lt;/p&gt;


&lt;div&gt;&amp;lt;object height=&quot;355&quot; width=&quot;425&quot;&gt;&amp;lt;param /&gt;&amp;lt;param /&gt;&amp;lt;param /&gt;&amp;lt;embed src=&quot;http://static.slideshare.net/swf/ssplayer2.swf?doc=metaprogramming-javascript-1195638729182448-3&quot; height=&quot;355&quot; width=&quot;425&quot;&gt;&amp;lt;/embed&gt;&amp;lt;/object&gt;&lt;div&gt;&lt;a href=&quot;http://www.slideshare.net/?src=embed&quot;&gt;&lt;img src=&quot;http://static.slideshare.net/swf/logo_embd.png&quot; alt=&quot;SlideShare&quot; /&gt;&lt;/a&gt; | &lt;a href=&quot;http://www.slideshare.net/danwrong/metaprogramming-javascript&quot; title=&quot;View 'Metaprogramming  JavaScript' on SlideShare&quot;&gt;View&lt;/a&gt; | &lt;a href=&quot;http://www.slideshare.net/upload&quot;&gt;Upload your own&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;

	&lt;p&gt;In other news, Low Pro 0.5 is nearly ready to rock.  The new version is quite a lot smaller as Prototype 1.6 has solved so many of the things I used to fix with Low Pro.  It does however have a few nice new features.  Watch this space.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://danwebb.net">
    <author>
      <name>dan</name>
    </author>
    <id>tag:danwebb.net,2007-10-07:2436</id>
    <published>2007-10-07T23:38:00Z</published>
    <updated>2007-12-21T23:41:28Z</updated>
    <category term="JavaScript"/>
    <category term="Low Pro"/>
    <category term="customattributes"/>
    <category term="dojo"/>
    <category term="javascript"/>
    <category term="lowpro"/>
    <link href="http://danwebb.net/2007/10/7/custom-attributes-and-class-names" rel="alternate" type="text/html"/>
    <title>Custom Attributes And Class Names</title>
<summary type="html">&lt;p&gt;At the Rich Web Experience, Alex Russell did a presentation entitled &lt;a href=&quot;http://alex.dojotoolkit.org/?p=622&quot;&gt;Standards Heresy&lt;/a&gt;, In the presentation, which I didn&#8217;t attend so forgive me if I&#8217;ve gotten the wrong end of the stick, he spoke about how Dojo has been forced to abandon web standards in some cases in order to get the job done.  The conversation was furthered by &lt;a href=&quot;http://www.easy-reader.net/archives/2007/09/10/alex-russell-is-not-a-heretic/&quot;&gt;Aaron Gustafson&#8217;s response&lt;/a&gt; and is a really interesting discussion.  However, I&#8217;m not going to comment on the politics of the &lt;span class=&quot;caps&quot;&gt;W3C&lt;/span&gt; further as I know nothing about that at all.  One example in the presentation reminded me of something I&#8217;ve wanted to comment on for a while now.  Here&#8217;s that example:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;html&quot;&gt;&amp;lt;div dojoType=&amp;quot;dijit.form.HorizontalSlider&amp;quot;
     name=&amp;quot;horizontal1&amp;quot;
     onChange=&amp;quot;dojo.byId('slider1input').value=arguments[0];&amp;quot;
     value=&amp;quot;10&amp;quot;
     maximum=&amp;quot;100&amp;quot;
     minimum=&amp;quot;0&amp;quot;
     showButtons=&amp;quot;false&amp;quot;
     intermediateChanges=&amp;quot;true&amp;quot;
     style=&amp;quot;width:50%; height: 20px;&amp;quot;
     id=&amp;quot;slider1&amp;quot;&amp;gt;
  ...
&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/pre&gt;</summary><content type="html">
            &lt;p&gt;At the Rich Web Experience, Alex Russell did a presentation entitled &lt;a href=&quot;http://alex.dojotoolkit.org/?p=622&quot;&gt;Standards Heresy&lt;/a&gt;, In the presentation, which I didn&#8217;t attend so forgive me if I&#8217;ve gotten the wrong end of the stick, he spoke about how Dojo has been forced to abandon web standards in some cases in order to get the job done.  The conversation was furthered by &lt;a href=&quot;http://www.easy-reader.net/archives/2007/09/10/alex-russell-is-not-a-heretic/&quot;&gt;Aaron Gustafson&#8217;s response&lt;/a&gt; and is a really interesting discussion.  However, I&#8217;m not going to comment on the politics of the &lt;span class=&quot;caps&quot;&gt;W3C&lt;/span&gt; further as I know nothing about that at all.  One example in the presentation reminded me of something I&#8217;ve wanted to comment on for a while now.  Here&#8217;s that example:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;html&quot;&gt;&amp;lt;div dojoType=&amp;quot;dijit.form.HorizontalSlider&amp;quot;
     name=&amp;quot;horizontal1&amp;quot;
     onChange=&amp;quot;dojo.byId('slider1input').value=arguments[0];&amp;quot;
     value=&amp;quot;10&amp;quot;
     maximum=&amp;quot;100&amp;quot;
     minimum=&amp;quot;0&amp;quot;
     showButtons=&amp;quot;false&amp;quot;
     intermediateChanges=&amp;quot;true&amp;quot;
     style=&amp;quot;width:50%; height: 20px;&amp;quot;
     id=&amp;quot;slider1&amp;quot;&amp;gt;
  ...
&amp;lt;/div&amp;gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is a snippet of code showing how Dojo is going off the standards rails by adding custom attributes that are &#8216;needed&#8217; to configure a slider widget.  This is a really good example of a case where you definitely shouldn&#8217;t add custom attributes to &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt;.  While the &lt;span class=&quot;caps&quot;&gt;W3C&lt;/span&gt;/browser vendors/whipping boy of choice might be letting us down what the web standards movement taught us (most importantly in my mind) is that semantic &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; is critically important.  Having a &amp;lt;div&amp;gt; with custom attributes that tell Dojo it&#8217;s a slider widget is of no use to anything apart from as a crutch to Dojo.  It has no semantic value at all.  Outside of the context of Dojo it&#8217;s just an useless lump of markup.&lt;/p&gt;


	&lt;p&gt;Although &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; is a pretty limited vocabulary which is very document centric we can still go much further than this in attempting to describe what this is semantically. It&#8217;s an input so use an input or a select box as the base element.  How browsers and assistive technologies deal with these elements is a lot closer to what we want than a meaningless &amp;lt;div&amp;gt;.  Secondly, there&#8217;s custom attributes &#8211; having attributes containing behavioural information like showButtons and intermediateChanges are equally as semantically useless as the old presentational attributes like bgcolor and border and we&#8217;ve all learnt the disadvantages of mixing content and presentation and discarded those&#8230;hopefully.  There&#8217;s got to be a better way.&lt;/p&gt;


	&lt;p&gt;So we want our semantic &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; and to keep our behaviour data out of our document.  Take a leaf out of &lt;span class=&quot;caps&quot;&gt;CSS&lt;/span&gt;&#8217;s book.  We can name and classify objects and use these as our hooks to attach our JavaScript powered behaviour.  Eureka! Not quite:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;html&quot;&gt;&amp;lt;!-- Created with http://cow.neondragon.net/stuff/reflection/reflectomatic.html  --&amp;gt;
&amp;lt;img src=&amp;quot;/reflection/photos/giraffe.jpg&amp;quot; width=&amp;quot;132&amp;quot; alt=&amp;quot;&amp;quot; class=&amp;quot;reflect ropacity71&amp;quot; /&amp;gt;&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The above code is from a nice little JavaScript effects library called &lt;a href=&quot;http://cow.neondragon.net/stuff/reflection/&quot;&gt;reflection.js&lt;/a&gt; which uses
class names to hook in it&#8217;s behaviour unobtrusively.  However, take a closer look at the class names.  They aren&#8217;t classifying things as such, they are embedding configuration into directly into the class names.  In a similar way it&#8217;s not a generally thought of as a good idea to give elements class names like &#8216;bigred&#8217; or &#8216;width40&#8217; although, depressingly, I&#8217;ve seen it done!  This is definitely an abuse of class names &#8211; its not a &#8216;bigred&#8217; its a &#8216;price&#8217; and so on.  This is seen not just in reflection.js but a large amount of &#8216;unobtrusive&#8217; libraries.&lt;/p&gt;


	&lt;p&gt;So how can we improve on this? We need to provide configuration information to our JavaScript but neither custom attributes or embedding info are looking good.  The answer is pretty simple &#8211; add another link to the chain and this is were (plug imminent :) projects like &lt;a href=&quot;http://www.bennolan.com/behaviour/&quot;&gt;behaviour.js&lt;/a&gt; and &lt;a href=&quot;/lowpro&quot;&gt;Low Pro&lt;/a&gt; come in.  They add that other link to the chain.  We can say that all inputs with the class &#8216;date&#8217; should have a date selector attached to them or that selects with the class &#8216;rating&#8217; should be replaced with a slider that&#8217;s range is 0-10 and so on.  Here&#8217;s a snippet using Low Pro to illustrate this:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Event.addBehavior({
  'select.rating': Slider({ min: 0, max: 10, intermediateValues: true }),
  'input.date': DateSelector
});&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;In this way we are able to maintain semantic &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt;, avoid adding custom attributes and maintain a very loose coupling between our document and it&#8217;s behaviour.  This, we&#8217;ve seen from &lt;span class=&quot;caps&quot;&gt;CSS&lt;/span&gt;, is vitally important to maintainability and a real advantage.  Via this method you can apply configuration to single items or as a group.  If you decided that &#8216;rating&#8217; selects should in fact all be vertical sliders instead of horizontal then that&#8217;s no problem either.  It gives you ultimate flexibility.&lt;/p&gt;


	&lt;p&gt;This methodology is right at the core of Low Pro and I&#8217;ve been getting incredible results from it.  It&#8217;s made what could be really complex JavaScript implementations a breeze.  Instead of a huge, fearsome application everything becomes a &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; document with many loosely, coupled smaller components attached to it.  If you don&#8217;t use Prototype/Low Pro don&#8217;t let that stop you from working this way.  &lt;a href=&quot;http://www.meme-rocket.com/2007/09/07/agnostic-unobtrusive-javascript/&quot;&gt;Bill Burcham wonders whether this can be library agnostic&lt;/a&gt;.  Well, as an idea, in a sense it already is. All you need is the ability to select by &lt;span class=&quot;caps&quot;&gt;CSS&lt;/span&gt; selectors.  Dojo and dijit are in fact very well suited.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://danwebb.net">
    <author>
      <name>dan</name>
    </author>
    <id>tag:danwebb.net,2007-08-21:2395</id>
    <published>2007-08-21T10:50:00Z</published>
    <updated>2007-08-21T11:14:20Z</updated>
    <category term="Events"/>
    <category term="JavaScript"/>
    <category term="dojo"/>
    <category term="events"/>
    <category term="javascript"/>
    <category term="jquery"/>
    <category term="prototype"/>
    <category term="yui"/>
    <link href="http://danwebb.net/2007/8/21/media-ajax-is-scarily-good" rel="alternate" type="text/html"/>
    <title>@media Ajax Is Scarily Good</title>
<content type="html">
            &lt;p&gt;&lt;img title=&quot;@media Ajax&quot; src=&quot;/assets/2007/6/10/amx_button.gif&quot; alt=&quot;@media Ajax&quot; /&gt; It seems that the &lt;a href=&quot;http://www.vivabit.com/atmediaAjax/&quot;&gt;@media Ajax site&lt;/a&gt; has just been updated.  I was looking forward to it before but more speakers have been added including Brendan Eich who, according the &lt;a href=&quot;http://www.vivabit.com/atmediaAjax/sessions/#javascript&quot;&gt;abstract,&lt;/a&gt; will be demonstrating a working version of Tamarin in Firefox!&lt;/p&gt;


	&lt;p&gt;Also, Dojo&#8217;s Alex Russell, jQuery&#8217;s John Resig, &lt;span class=&quot;caps&quot;&gt;YUI&lt;/span&gt;&#8217;s Douglas Crockford and Prototype&#8217;s erm, me will be representing the major libraries and then there&#8217;s the Ajaxians, Ben and Dion stepping up for a keynote.  Finally, Mr Jeremy Keith will be hosting the hot topics panel in the traditional @media style &#8211; Hot Topics panel + JavaScript people should be quite explosive.  As it was put in the Prototype Core Campfire the other day, &#8220;The JavaScript community is not so much of a community, more of a debating society&#8221;  It&#8217;s going to be excellent.  Of course, as it&#8217;s in London town you know we&#8217;ll be sinking a good few pints after.  It will be a rare chance to get together with fellow scriptie types this side of the pond and the only pub conversation you&#8217;ll ever be allowed to mention closures in&#8230;.surely that&#8217;s a good thing?&lt;/p&gt;


	&lt;p&gt;There are still early bird tickets left until the end of august so &lt;a href=&quot;http://www.vivabit.com/atmediaAjax/register/&quot;&gt;get your arse down to the site and sign up.&lt;/a&gt;&lt;/p&gt;


	&lt;p&gt;My presentation, &lt;a href=&quot;http://www.vivabit.com/atmediaAjax/sessions/#metaprogramming&quot;&gt;Metaprogramming JavaScript,&lt;/a&gt; is for all those people who moan that they never learn things at conferences.  It&#8217;s journey into the deep innards of JavaScript and I&#8217;ll be explaining the some of the metaprogramming techniques used in &lt;span class=&quot;caps&quot;&gt;YUI&lt;/span&gt;, Prototype and Low Pro along with really getting into advanced JavaScript concepts like prototype inheritance, functional programming and closures.  I defy you to turn up and not learn something new.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://danwebb.net">
    <author>
      <name>dan</name>
    </author>
    <id>tag:danwebb.net,2007-07-18:2328</id>
    <published>2007-07-18T14:30:00Z</published>
    <updated>2007-07-18T14:32:45Z</updated>
    <category term="JavaScript"/>
    <category term="Low Pro"/>
    <category term="javascript"/>
    <category term="lowpro"/>
    <category term="prototype"/>
    <category term="tutorial"/>
    <link href="http://danwebb.net/2007/7/18/low-pro-behaviours-101-part-2" rel="alternate" type="text/html"/>
    <title>Low Pro Behaviours 101: Part 2</title>
<summary type="html">&lt;p&gt;In the first post I briefly explained how to use pre-made Low Pro behaviours but, although I&#8217;m in the process of &lt;a href=&quot;http://svn.danwebb.net/external/lowpro/trunk/behaviours/&quot;&gt;writing a few myself&lt;/a&gt;, Low Pro is not going to be a widget library &#8211; what it is is a great framework to write your own components with.  So, in this post, I&#8217;m going to go into a little detail about how to  write your own behaviours.  If the idea of that doesn&#8217;t bore your rigid then read on&#8230;&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;In the first post I briefly explained how to use pre-made Low Pro behaviours but, although I&#8217;m in the process of &lt;a href=&quot;http://svn.danwebb.net/external/lowpro/trunk/behaviours/&quot;&gt;writing a few myself&lt;/a&gt;, Low Pro is not going to be a widget library &#8211; what it is is a great framework to write your own components with.  So, in this post, I&#8217;m going to go into a little detail about how to  write your own behaviours.  If the idea of that doesn&#8217;t bore your rigid then read on&#8230;&lt;/p&gt;
&lt;h3&gt;Creating a behaviour&lt;/h3&gt;


	&lt;p&gt;As discussed in the last post behaviours are actually just specialised constructor functions.  So, to create our behaviour we use Behavior.create in a very similar way to Class.create:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;var Hover = Behavior.create();&lt;/code&gt;&lt;/pre&gt;

	&lt;h3&gt;Adding methods and properties&lt;/h3&gt;


	&lt;p&gt;This creates an empty behaviour much the same as Class.create gives us an empty class. Now we add our methods on to the prototype of the constructor that we&#8217;ve created.  This example is a super simple behaviour, Hover, that will add/remove a class name (which you can specify) on mouse over/out:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Object.extend(Hover.prototype, {
  initialize: function(className) {
    this.className = className || 'over';
  },
  onmouseover: function() {
    this.element.addClassName(this.className);
  },
  onmouseout: function() {
    this.element.removeClassName(this.className);
  }
});&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Any on* methods are bound as event handlers to the element when the behavior is attached to the element.  The scope of these methods is &#8216;corrected&#8217; automatically so that this will always refer to the behaviour instance.  The behaviour instance also has a magic property, this.element, which points to the element it is attached to.  The final thing you need to know is that the initialize method is called as soon as the &lt;span class=&quot;caps&quot;&gt;DOM&lt;/span&gt; is ready (as soon as the element is available).  We can now use this behaviour in the manner discussed in the last post:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Event.addBehavior({
  '.dongle': Hover('hover')
});&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Aside from these special cases behaviour constructors are identical to any regular JavaScript &#8216;class&#8217;.  You can add methods and properties as you see fit.  Note that each element that has the behaviour attached to it gets its own instance of the behaviour so you can maintain the state of each behaviour independently without having to resort to setting expandos on the element, using closures or any other method which is messy and prone to causing those nasty circular references that cause memory leaks.  As an alternative you can just pass your methods straight into the Behavior.create call so the following is equivilent to the above code:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;var Hover = Behavior.create({
  initialize: function(className) {
    this.className = className || 'over';
  },
  onmouseover: function() {
    this.element.addClassName(this.className);
  },
  onmouseout: function() {
    this.element.removeClassName(this.className);
  }
});&lt;/code&gt;&lt;/pre&gt;

	&lt;h3&gt;Working with multiple elements&lt;/h3&gt;


	&lt;p&gt;Now this is all good for dealing with behaviours that work on a single element but in many cases we want a behaviour to operate on a number of elements.  For example, you would want to be able to attach a Sortable behaviour to a list element but the behaviour itself would also need to deal with all the list element inside it.  There are a number of ways to approach writing these kind of behaviours, all of which are demonstrated in the various test behaviours I&#8217;ve written.&lt;/p&gt;


	&lt;h4&gt;Event delegation with behaviours&lt;/h4&gt;


	&lt;p&gt;The first technique, and normally the best is using event delegation to capture all the events on elements inside the attached element with your behaviour and process them.  The process is pretty simple&#8230;here&#8217;s part of the &lt;a href=&quot;http://svn.danwebb.net/external/lowpro/trunk/behaviours/date_selector.js&quot;&gt;Calendar&lt;/a&gt; behaviour:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;var Calendar = Behavior.create({
  // other methods hidden for clarity
  onclick : function(e) {
    var source = Event.element(e);
    Event.stop(e);

    if ($(source.parentNode).hasClassName('day')) return this._setDate(source);
    if ($(source.parentNode).hasClassName('back')) return this._backMonth();
    if ($(source.parentNode).hasClassName('forward')) return this._forwardMonth();
  }
});&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Here, we define a single onclick method which will capture clicks on all the elements inside the calendar &#8211; Forward and back arrows, and the dates themselves.  Firstly, we use Event.element to grab the source element, the element that was actually clicked, then we can just examine this element to work out what needs to happen to the calendar and call the relevent method.  In this case, and in most cases, the most convenient way is to look at what class names the element has.  In the calendar, each day has the class &#8216;day&#8217; and the forward/back arrows have the classes &#8216;forward&#8217; and &#8216;back&#8217;.  This method is really nice for a number of reasons, firstly, we avoid attaching lots of event handlers for each sub element and secondly we have this central point where we can handle and dispatch events &#8211; a really pleasant way to work with complex interactions. Of course, event delegation isn&#8217;t always a good fit so&#8230;&lt;/p&gt;


	&lt;h4&gt;Linking behaviours&lt;/h4&gt;


	&lt;p&gt;In more complex cases when the sub elements of your behaviour need to maintain their own state or handle events in a more complex way you can have your main behaviour attach sub-behaviours to inner elements during initialisation and maintain a link between the two.  An example of this can be seen in the &lt;a href=&quot;http://svn.danwebb.net/external/lowpro/trunk/behaviours/draggable.js&quot;&gt;Draggable&lt;/a&gt; behaviour:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Draggable = Behavior.create({
  initialize : function(options) {
    // code hidden for clarity
    this.handle = this.options.handle || this.element;
    new Draggable.Handle(this.handle, this);
  }
  // code hidden for clarity
});

Draggable.Handle = Behavior.create({
  initialize : function(draggable) {
    this.draggable = draggable;
  },
  onmousedown : function(e) {
        // code hidden for clarity
  }
});&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Here, the initialize method of Draggable finds the element that it wants to be the &#8216;drag handle&#8217; and attaches another behaviour, Draggable.Handle, to it.  At this point we pass a reference to the Draggable instance to the Draggable.Handle instance so the drag handle can refer back to and control the main draggable behaviour.  You can of course, if necessary, build up a complex component by building a number of linked behaviours that all comunicate back to the main behaviour.&lt;/p&gt;


	&lt;h3&gt;Get it? Got it? Good&lt;/h3&gt;


	&lt;p&gt;So that&#8217;s all for now.  Any questions just drop me a comment and I&#8217;ll get back to you &lt;span class=&quot;caps&quot;&gt;ASAP&lt;/span&gt;.  At this point, if you are interested in writing your own behaviours then it would be worth referring to the &lt;a href=&quot;http://svn.danwebb.net/external/lowpro/trunk/behaviours/&quot;&gt;behaviours in &lt;span class=&quot;caps&quot;&gt;SVN&lt;/span&gt;&lt;/a&gt;.  However, I&#8217;m just feeling this out for myself and I&#8217;m sure I&#8217;ve not explored any near all of the possibilities of working with behaviours so if you have any brain waves be sure to post a comment also.  If there&#8217;s enough interest I might include a behaviours repository on the upcoming Low Pro site.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://danwebb.net">
    <author>
      <name>dan</name>
    </author>
    <id>tag:danwebb.net,2007-07-17:2318</id>
    <published>2007-07-17T01:02:00Z</published>
    <updated>2007-07-17T02:18:59Z</updated>
    <category term="JavaScript"/>
    <category term="Low Pro"/>
    <category term="javascript"/>
    <category term="lowpro"/>
    <category term="prototype"/>
    <category term="tutorial"/>
    <link href="http://danwebb.net/2007/7/17/low-pro-behaviours-101" rel="alternate" type="text/html"/>
    <title>Low Pro Behaviours 101</title>
<content type="html">
            &lt;p&gt;Okay, I&#8217;ve gone on about this for long enough and made lots of promises.  So now&#8217;s time to make a start on explaining all this business with Low Pro and &#8216;behaviours&#8217;.&lt;/p&gt;


	&lt;p&gt;Behaviours are part of Low Pro and are object orientated mechanism by which you can handle events and maintain the state of an element (or indeed a set of elements) and as I&#8217;ve found out recently are an incredibly nice way of writing and deploying UI widgets.  Before I dive in to how to write your own behaviours, let&#8217;s get a flavour for how to use them.  If you want to play along you&#8217;ll need the latest versions of Prototype and Low Pro as well as the remote behaviours which you can &lt;a href=&quot;http://svn.danwebb.net/external/lowpro/trunk/behaviours/remote.js&quot;&gt;grab from my &lt;span class=&quot;caps&quot;&gt;SVN&lt;/span&gt;&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;The first thing to note about a behaviour is that they are simply specialised constructor functions so you can use them with the new keyword to create an instance:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;var aLink = $('edit_5');
var ajaxLink = new Remote.Link(aLink, { method: 'post' });&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Behaviours, when used with the new keyword, always take an element reference as the first argument.  Any subsequent arguments are passed to the behaviours constructor.  In the case above, Remote.Link is a behavior which when attached to a link will make the link trigger an Ajax request rather than a tradtional page refresh. With this behaviour the second argument is an options hash that is passed through to the Ajax request.  Now we&#8217;ve created an instance of the behaviour that instance of the behaviour is bound to that element for it&#8217;s life time on the page handling click events on the link and sending Ajax requests.&lt;/p&gt;


	&lt;p&gt;If you need to attach a behaviour to multiple elements on then using new could get a little tedious.  To this end behaviours work very nicely with Event.addBehavior:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Event.addBehavior({
  'a.edit': Remote.Link
});&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Using a behaviour with Event.addBehavior in this way will attach a seperate instance of the behavior to every element that matches the &lt;span class=&quot;caps&quot;&gt;CSS&lt;/span&gt; selector.  Notice we don&#8217;t pass in an event in the selector as you might be used to.  The behaviour knows what events it wants to listen for.  Now all the links in the page with the class name &#8216;edit&#8217; will invoke Ajax request.  You may need to pass in extra information to the behaviour as we did above with the options hash.  Thanks to a bit of metaprogramming hackery we can do this too:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Event.addBehavior({
  'a.edit': Remote.Link({ onComplete: triggerEdit })
});&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;As you can see, it&#8217;s almost like &lt;span class=&quot;caps&quot;&gt;CSS&lt;/span&gt; for interactivity where you&#8217;re defining how elements behave in an declaritive, almost code-less fashion. In my own projects I&#8217;ve gained massive advances in maintainability by separating everything out into behaviours then using Event.addBehavior as the glue to my &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt;.  You don&#8217;t need to use custom &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; attributes or weird classnames &#8211; you just bind behaviours to elements as you would style rules in &lt;span class=&quot;caps&quot;&gt;CSS&lt;/span&gt;.&lt;/p&gt;


	&lt;p&gt;Of course, using this method you don&#8217;t get references to the behaviour instances you&#8217;ve created.  In fact, you&#8217;ll hardly ever need to get references to these behaviour instances as most behaviors don&#8217;t need public methods at all but in case you need to get at them there&#8217;s a property of each behavior that contains an array of all its instances on the page:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Remote.Link.instances //=&amp;gt; an array of all the instances of Remote.Link&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;So that&#8217;s basically how to use behaviours.  In a day or two I&#8217;ll post part two of Behaviours 101 which will show you how to make your own but in the meantime try out some of the behaviours I&#8217;ve already created.  So far there&#8217;s a Draggable, Droppable, A date picker, Remote link and Remote form.  Check out the &lt;a href=&quot;/behavior_demo/&quot;&gt;demo page&lt;/a&gt;.&lt;/p&gt;
          </content>  </entry>
  <entry xml:base="http://danwebb.net">
    <author>
      <name>dan</name>
    </author>
    <id>tag:danwebb.net,2007-06-16:2064</id>
    <published>2007-06-16T15:19:00Z</published>
    <updated>2007-06-17T03:29:15Z</updated>
    <category term="JavaScript"/>
    <category term="Low Pro"/>
    <category term="Projects"/>
    <category term="Rails"/>
    <category term="javascript"/>
    <category term="lengthy"/>
    <category term="lowpro"/>
    <category term="plugins"/>
    <category term="ujs4rails"/>
    <link href="http://danwebb.net/2007/6/16/the-state-and-future-of-the-ujs-plugin" rel="alternate" type="text/html"/>
    <title>The State (And Future) Of The UJS Plugin</title>
<summary type="html">&lt;p&gt;Over the past few weeks loads of people have been asking me about what&#8217;s going on the &lt;span class=&quot;caps&quot;&gt;UJS&lt;/span&gt; plugin.  It&#8217;s obviously fallen in to disrepair so people, understandably, have been concerned.  There has been a reason for this aside from the fact that I am a lazy barsteward (which of course I am, but that&#8217;s beside the point).  Here is a letter I&#8217;ve just written to the &lt;span class=&quot;caps&quot;&gt;UJS&lt;/span&gt; mailing list that I&#8217;d thought I&#8217;d post here to try to get a little more feedback.  It&#8217;s a little bit long but bear with me&#8230;&lt;/p&gt;</summary><content type="html">
            &lt;p&gt;Over the past few weeks loads of people have been asking me about what&#8217;s going on the &lt;span class=&quot;caps&quot;&gt;UJS&lt;/span&gt; plugin.  It&#8217;s obviously fallen in to disrepair so people, understandably, have been concerned.  There has been a reason for this aside from the fact that I am a lazy barsteward (which of course I am, but that&#8217;s beside the point).  Here is a letter I&#8217;ve just written to the &lt;span class=&quot;caps&quot;&gt;UJS&lt;/span&gt; mailing list that I&#8217;d thought I&#8217;d post here to try to get a little more feedback.  It&#8217;s a little bit long but bear with me&#8230;&lt;/p&gt;
&lt;p&gt;I&#8217;ve been chatting to Luke and users of &lt;span class=&quot;caps&quot;&gt;UJS&lt;/span&gt; about what to do with it and still haven&#8217;t quiet decided hence the lack of news but below is a rundown of where we are at on the whole thing.  However, this is definitely personal opinion and doesn&#8217;t necessarily represent Luke&#8217;s opinion on the matter.&lt;/p&gt;


	&lt;p&gt;Essentially, the status is that, of late, I personally have not used &lt;span class=&quot;caps&quot;&gt;UJS&lt;/span&gt; at all and have found a much better process by using Low Pro on its own without all the Ruby scaffolding of the &lt;span class=&quot;caps&quot;&gt;UJS&lt;/span&gt; plugin.  Secondarily, after talking to lots of developers at RailsConf it seems that the &lt;span class=&quot;caps&quot;&gt;UJS&lt;/span&gt; plugin has failed to truly achieve it&#8217;s main goal which is to get Rails developers to write JavaScript using progressive enhancement.  Many people seem to mainly use the plugin to get their JavaScript in to a separate file which is actually not even essential to progressive enhancement and I think this is a failing in the design of &lt;span class=&quot;caps&quot;&gt;UJS&lt;/span&gt; itself.  To achieve progressive enhancement you really need to think of JavaScript as a separate layer on top of a working &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; application but &lt;span class=&quot;caps&quot;&gt;UJS&lt;/span&gt; lets you get away with keeping behavior in your views and hence leads many developers to think in the same way as they did before but think they are unobtrusive because they don&#8217;t see any JavaScript in their &lt;span class=&quot;caps&quot;&gt;HTML&lt;/span&gt; &#8211; which is obviously not what we wanted to achieve.  While many people can and do successfully use &lt;span class=&quot;caps&quot;&gt;UJS&lt;/span&gt; for progressive enhancement even more seem not to &#8211; &lt;span class=&quot;caps&quot;&gt;UJS&lt;/span&gt; has not been the &#8216;angel on your shoulder&#8217; that I originally wanted it to be.&lt;/p&gt;


	&lt;p&gt;On top of this, the method by which the generated JavaScript is kept in the session has many limitations which myself and Luke have been aware of from the start.  It&#8217;s not generally a good idea to keep this much information in the session (in fact, normally I never store more than a user ID if I can help it) and while acceptable for light to medium use it does have an upper limit depending on the type of session storage you are using.  Rails edge now uses cookies to store session info by default which have a very very low limit which will cripple &lt;span class=&quot;caps&quot;&gt;UJS&lt;/span&gt; completely.  We have considered other alternatives such as some kind of file based storage but every time it just strikes me as too much scaffolding just to allow developers to put behavior in their view files which, as a said above, I&#8217;ve come to believe is a really bad idea anyway.&lt;/p&gt;


	&lt;p&gt;One of the things that I do personally like and something that has received the most positive feedback are the behavior helpers (the make_*) stuff which essentially encapsulate common tasks with sensible defaults in a very Rails like way.  It&#8217;s a real time saver and the conventions provided mean that the best path (such as using this.href for the Ajax url) is the easiest.  Recently, I&#8217;ve come to do this in my own projects via Low Pro and it&#8217;s behavior &#8216;classes&#8217; (although they need a better name!).  Now via Low Pro I can write stuff like:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Event.addBehavior({
  '.product a.description' : Remote.Link({ update : 'product_description' }),
  '.product' : Draggable({ revert : true }),
  '#basket' : Droppable
});&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;I really like this and am slowly building up a library (which you can see if you look at the &lt;a href=&quot;http://svn.danwebb.net/external/lowpro/trunk/behaviors/&quot;&gt;Low Pro trunk&lt;/a&gt;) of common behaviors.  There&#8217;s a date picker, a drag/drop implementation and the remote stuff I&#8217;ve illustrated above.  I&#8217;m planning on writing autocompleters and in-place editors as behaviors as well.  But behaviors have proven really easy to write and I love them as a tool for building site specific components.  In fact, as I&#8217;ve worked with Low Pro it&#8217;s become apparent to me that behaviors are by far the killer feature which is interesting as they were just an experiment I hacked together one day without much thought.&lt;/p&gt;


	&lt;p&gt;So what to do?  Well, there&#8217;s two ways to go as far as I can see.  The first is to shut down development on &lt;span class=&quot;caps&quot;&gt;UJS&lt;/span&gt; completely (or hand it over to another party if anyone is interested) and go on to promote the techniques of implementing progressive enhancement using Low Pro that I&#8217;ve found to be so successful recently.  This could possibly be via the UJS4Rails site or through my own site &#8211; I&#8217;m not sure which would be a better platform right now.&lt;/p&gt;


	&lt;p&gt;The other would be to re-think the &lt;span class=&quot;caps&quot;&gt;UJS&lt;/span&gt; plugin totally and go for some kind of 2.0 release that would take a completely different tack.  However, all of the ideas for this I&#8217;ve thought of or heard so far don&#8217;t really compel me to write them.  I think to work on this myself I&#8217;d need to be sure that I&#8217;d want to use it and so far this is not the case but any ideas and feedback are very welcome so please do drop me a mail or feedback on this list.&lt;/p&gt;


	&lt;p&gt;Either way, we need to make fixes to the current plugin to make it work with Rails 1.2.3 which I&#8217;ve been working on recently but I&#8217;d love patches if you&#8217;ve already solved these issues yourself (which it appears many of you have).&lt;/p&gt;


	&lt;p&gt;So yes, that&#8217;s it. Let me know what you think, I&#8217;d appreciate any feedback you have.&lt;/p&gt;
          </content>  </entry>
</feed>

