<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>James Padolsey&#187; HTML/XHTML category &#8211; James Padolsey</title>
	<atom:link href="http://james.padolsey.com/category/html/feed/" rel="self" type="application/rss+xml" />
	<link>http://james.padolsey.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sun, 22 Jan 2012 17:04:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>It&#8217;s just an API</title>
		<link>http://james.padolsey.com/javascript/its-just-an-api/</link>
		<comments>http://james.padolsey.com/javascript/its-just-an-api/#comments</comments>
		<pubDate>Tue, 09 Jun 2009 11:53:32 +0000</pubDate>
		<dc:creator>James</dc:creator>
				<category><![CDATA[HTML/XHTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[DOM]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://james.padolsey.com/?p=916</guid>
		<description><![CDATA[Thousands of developers interact with this API on a daily basis yet most forget it is <em>just</em>&#8230; an API! Of course, I&#8217;m talking about the notorious &#8220;<a href="http://en.wikipedia.org/wiki/Document_Object_Model">Document Object Model</a>&#8221; (a.k.a the DOM). With the rise of &#8220;Rich Internet Applications&#8221; interaction with the DOM has sky-rocketed&#8230;]]></description>
			<content:encoded><![CDATA[<p>Thousands of developers interact with this API on a daily basis yet most forget it is <em>just</em>&#8230; an API! Of course, I&#8217;m talking about the notorious &#8220;<a href="http://en.wikipedia.org/wiki/Document_Object_Model">Document Object Model</a>&#8221; (a.k.a the DOM). With the rise of &#8220;Rich Internet Applications&#8221; interaction with the DOM has sky-rocketed and as a result it&#8217;s being heavily abused!</p>

<p>Have you ever needed to do this? -</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="co1">// E.g. 1</span>
element.<span class="me1">addEventListener</span><span class="br0">&#40;</span><span class="st0">'click'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span><span class="br0">&#123;</span>
    <span class="kw1">this</span>.<span class="me1">rel</span> <span class="sy0">+=</span> <span class="st0">' clicked'</span> <span class="sy0">+</span> <span class="st0">'('</span> <span class="sy0">+</span> e.<span class="me1">clientX</span> <span class="sy0">+</span> <span class="st0">' ,'</span> <span class="sy0">+</span> e.<span class="me1">clientY</span> <span class="sy0">+</span> <span class="st0">')'</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="co1">// E.g. 2</span>
jQuery<span class="br0">&#40;</span><span class="st0">'a'</span><span class="br0">&#41;</span>.<span class="me1">mouseover</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#123;</span>
    <span class="kw2">var</span> color <span class="sy0">=</span> jQuery<span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>.<span class="me1">css</span><span class="br0">&#40;</span><span class="st0">'color'</span><span class="br0">&#41;</span><span class="sy0">;</span>
    jQuery<span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span>.<span class="me1">data</span><span class="br0">&#40;</span><span class="st0">'beforeColor'</span><span class="sy0">,</span> color<span class="br0">&#41;</span><span class="sy0">;</span>
    changeColor<span class="br0">&#40;</span><span class="kw1">this</span><span class="br0">&#41;</span><span class="sy0">;</span>
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>If you have then (in my humble opinion) you&#8217;re abusing the DOM!</p>

<p>The first example is obviously bad practice; using HTML attributes to track changes unrelated to the HTML content is a crime of sorts and should never be done! The second example probably doesn&#8217;t seem as bad to most developers. jQuery&#8217;s &#8220;data&#8221; method allows you to store expando-like data about an element without obtrusively defining it as a direct property. This technique of storing element data is far better than using the HTML <code>rel</code> attribute. Or is it?</p>

<p>I was an advocate of this particular technique but I&#8217;ve come to realise that it is, in most situations, no better than using any random HTML attribute. If we put the unobtrusive-vs-obtrusive argument aside, both techniques are effectively the same. Plus, jQuery&#8217;s &#8220;data&#8221; technique is not <em>really</em> unobtrusive &#8211; jQuery still uses an expando property to tie the element to a particular property in a JavaScript object; I&#8217;m not saying there&#8217;s anything wrong with this, I&#8217;m just pointing out the common misconception that using jQuery&#8217;s <code>data</code> method is somehow cleaner than using a random or made-up HTML attribute (/DOM property).</p>

<p>The main reason I am now so sceptical about its usage is because, over time, I&#8217;ve seen a lot of cases that don&#8217;t really require it at all and could quite easily be achieved with a well-thought-out use of JavaScript closures. Obviously it&#8217;s not all bad; there are a few valid uses &#8211; for example, jQuery currently uses its own &#8220;data&#8221; method to tie events to elements/objects; a central part of the its event dispatch system.</p>

<h2>Magical closures</h2>

<p>The nature of JavaScript closures allows us to use regular variables to store progress or states and then access those variables from within nested functions. I&#8217;ve put together a short example; this is an outline of a drag-and-drop script:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span class="co1">// Library neutral/agnostic</span>
&nbsp;
draggableElements.<span class="me1">forEach</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span>elem<span class="br0">&#41;</span><span class="br0">&#123;</span>
&nbsp;
    <span class="kw2">var</span> down <span class="sy0">=</span> <span class="kw2">false</span><span class="sy0">,</span>
        y <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">,</span>
        x <span class="sy0">=</span> <span class="nu0">0</span><span class="sy0">;</span>
&nbsp;
    elem.<span class="me1">bind</span><span class="br0">&#40;</span><span class="st0">'mousedown'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span><span class="br0">&#123;</span>
        down <span class="sy0">=</span> <span class="kw2">true</span><span class="sy0">;</span>
        x <span class="sy0">=</span> e.<span class="me1">pageX</span> <span class="sy0">-</span> getOffset<span class="br0">&#40;</span>elem<span class="br0">&#41;</span>.<span class="me1">left</span><span class="sy0">;</span>
        y <span class="sy0">=</span> e.<span class="me1">pageY</span> <span class="sy0">-</span> getOffset<span class="br0">&#40;</span>elem<span class="br0">&#41;</span>.<span class="me1">top</span><span class="sy0">;</span>
    <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
    document.<span class="me1">bind</span><span class="br0">&#40;</span><span class="st0">'mousemove'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span><span class="br0">&#123;</span>
        <span class="kw1">if</span> <span class="br0">&#40;</span>down<span class="br0">&#41;</span> <span class="br0">&#123;</span>
            elem.<span class="me1">applyCSS</span><span class="br0">&#40;</span><span class="br0">&#123;</span>
                left<span class="sy0">:</span> e.<span class="me1">pageX</span> <span class="sy0">-</span> x<span class="sy0">,</span>
                top<span class="sy0">:</span> e.<span class="me1">pageY</span> <span class="sy0">-</span> y
            <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
        <span class="br0">&#125;</span>
    <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
    document.<span class="me1">bind</span><span class="br0">&#40;</span><span class="st0">'mouseup'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span>e<span class="br0">&#41;</span><span class="br0">&#123;</span>
        down <span class="sy0">=</span> <span class="kw2">false</span><span class="sy0">;</span>
    <span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
<span class="br0">&#125;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>




<p>Every &#8220;draggable&#8221; element is given its very own closure (through <code>forEach</code>) within which we can define numerous variables accessible in each of the nested &#8220;handler&#8221; functions (&#8216;mousemove&#8217;, &#8216;mousedown&#8217;, etc.). No expando properties are required, <em>nada</em>!</p>

<p>I haven&#8217;t got much more to say about this; the above code pretty much sums up my point!</p>

<p>I&#8217;m interested in valid uses of jQuery&#8217;s <code>data()</code> method (other than those used in the core); someone please enlighten me!</p>]]></content:encoded>
			<wfw:commentRss>http://james.padolsey.com/javascript/its-just-an-api/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

