<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: It&#8217;s just an API</title>
	<atom:link href="http://james.padolsey.com/javascript/its-just-an-api/feed/" rel="self" type="application/rss+xml" />
	<link>http://james.padolsey.com/javascript/its-just-an-api/</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Thu, 02 Feb 2012 18:03:26 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
	<item>
		<title>By: Sean McArthur</title>
		<link>http://james.padolsey.com/javascript/its-just-an-api/comment-page-1/#comment-13906</link>
		<dc:creator>Sean McArthur</dc:creator>
		<pubDate>Tue, 09 Jun 2009 19:10:45 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=916#comment-13906</guid>
		<description>The way MooTools handles it, using Element#store and Element#retrieve, those functions were defined in a closure, with an object called &lt;code&gt;storage&lt;/code&gt;.

All Elements are given a UID when returned from $ or $$, and that UID is used as a key for the &lt;code&gt;storage&lt;/code&gt; object.  So they essentially use what you suggested to Ben for you.</description>
		<content:encoded><![CDATA[<p>The way MooTools handles it, using Element#store and Element#retrieve, those functions were defined in a closure, with an object called <code>storage</code>.</p>
<p>All Elements are given a UID when returned from $ or $$, and that UID is used as a key for the <code>storage</code> object.  So they essentially use what you suggested to Ben for you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pete</title>
		<link>http://james.padolsey.com/javascript/its-just-an-api/comment-page-1/#comment-13902</link>
		<dc:creator>Pete</dc:creator>
		<pubDate>Tue, 09 Jun 2009 15:35:25 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=916#comment-13902</guid>
		<description>The way libraries are doing this is pretty solid: e.g The custom attribute in jquery is &#039;jQuery&#039; + timestamp with an integer value attached. 

That&#039;s certainly safer in memory management terms than attaching a indefinite ammount of objects and functions to elements. And unique enough to be considered unobtrusive imo.

I think you&#039;re right though that storing data on elements can usually be avoided, and should be a second choice.</description>
		<content:encoded><![CDATA[<p>The way libraries are doing this is pretty solid: e.g The custom attribute in jquery is &#8216;jQuery&#8217; + timestamp with an integer value attached. </p>
<p>That&#8217;s certainly safer in memory management terms than attaching a indefinite ammount of objects and functions to elements. And unique enough to be considered unobtrusive imo.</p>
<p>I think you&#8217;re right though that storing data on elements can usually be avoided, and should be a second choice.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben Nadel</title>
		<link>http://james.padolsey.com/javascript/its-just-an-api/comment-page-1/#comment-13896</link>
		<dc:creator>Ben Nadel</dc:creator>
		<pubDate>Tue, 09 Jun 2009 13:48:28 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=916#comment-13896</guid>
		<description>@James,

Yeah, I suppose a unified solution would cut down on a need for data. However, for something that controls elements in which the HTML itself is dynamic (ex. a paging data-grid), I think there is something quite elegant about keeping the meta-data with the record itself such that when the new HTML is injected / replaced, the old meta-data is stripped out along with it.</description>
		<content:encoded><![CDATA[<p>@James,</p>
<p>Yeah, I suppose a unified solution would cut down on a need for data. However, for something that controls elements in which the HTML itself is dynamic (ex. a paging data-grid), I think there is something quite elegant about keeping the meta-data with the record itself such that when the new HTML is injected / replaced, the old meta-data is stripped out along with it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James</title>
		<link>http://james.padolsey.com/javascript/its-just-an-api/comment-page-1/#comment-13895</link>
		<dc:creator>James</dc:creator>
		<pubDate>Tue, 09 Jun 2009 13:17:27 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=916#comment-13895</guid>
		<description>@Ryan, looks interesting; I haden&#039;t heard of those HTML5 data attributes. One question about your usage of it: I&#039;m not quite sure why the method is being stored as a string and then eval&#039;d. Why can&#039;t it just be stored as an *actual* method so that it can be called like any other method? Maybe I&#039;m missing the point...

@Dan &amp; @Ben, So it seems that &lt;code&gt;data()&lt;/code&gt; is mostly used to create &quot;linkers&quot; between different abstractions; notably between jQuery plugins and the code that makes use of them (or, other jQuery plugins). A unified abstraction would not require these linkers.

@Ben, is it not possible for you to have that extra information for each table row existing somewhere else, like in a JavaScript object/array (one with a structure similar to the table?). Also, I don&#039;t see how using &lt;code&gt;data()&lt;/code&gt; cuts down on the amount of functions/methods required? Even with my example, the anonymous &lt;code&gt;forEach&lt;/code&gt; function is only being created once; a new closure is created every time it runs. 

Thanks for the comments, I really appreciate the insight! :)</description>
		<content:encoded><![CDATA[<p>@Ryan, looks interesting; I haden&#8217;t heard of those HTML5 data attributes. One question about your usage of it: I&#8217;m not quite sure why the method is being stored as a string and then eval&#8217;d. Why can&#8217;t it just be stored as an *actual* method so that it can be called like any other method? Maybe I&#8217;m missing the point&#8230;</p>
<p>@Dan &#038; @Ben, So it seems that <code>data()</code> is mostly used to create &#8220;linkers&#8221; between different abstractions; notably between jQuery plugins and the code that makes use of them (or, other jQuery plugins). A unified abstraction would not require these linkers.</p>
<p>@Ben, is it not possible for you to have that extra information for each table row existing somewhere else, like in a JavaScript object/array (one with a structure similar to the table?). Also, I don&#8217;t see how using <code>data()</code> cuts down on the amount of functions/methods required? Even with my example, the anonymous <code>forEach</code> function is only being created once; a new closure is created every time it runs. </p>
<p>Thanks for the comments, I really appreciate the insight! <img src='http://james.padolsey.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben Nadel</title>
		<link>http://james.padolsey.com/javascript/its-just-an-api/comment-page-1/#comment-13891</link>
		<dc:creator>Ben Nadel</dc:creator>
		<pubDate>Tue, 09 Jun 2009 12:36:38 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=916#comment-13891</guid>
		<description>Not that this is something that I truly every think about, but I would think that by using the data() method, it allows us to create one set of functions for the entire set of target elements, rather than a single closure for each element in the target set. Data(), essentially, allows us to be slightly more memory sensitive as it cuts down on the number of methods that are defined.

I agree also with where Dan is coming from. I am working on some data-table stuff right now that uses plugins and I am using the data() method to bind record-specific data to each tbody element such that other plugins used in the record initialization will know how to access record-specific data.</description>
		<content:encoded><![CDATA[<p>Not that this is something that I truly every think about, but I would think that by using the data() method, it allows us to create one set of functions for the entire set of target elements, rather than a single closure for each element in the target set. Data(), essentially, allows us to be slightly more memory sensitive as it cuts down on the number of methods that are defined.</p>
<p>I agree also with where Dan is coming from. I am working on some data-table stuff right now that uses plugins and I am using the data() method to bind record-specific data to each tbody element such that other plugins used in the record initialization will know how to access record-specific data.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan G. Switzer, II</title>
		<link>http://james.padolsey.com/javascript/its-just-an-api/comment-page-1/#comment-13890</link>
		<dc:creator>Dan G. Switzer, II</dc:creator>
		<pubDate>Tue, 09 Jun 2009 12:11:39 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=916#comment-13890</guid>
		<description>Where I think the data() method is useful is when you need to associate a value with a DOM element that needs to be publicly available. For example, you&#039;re developing two companion plug-ins that need to work with each other. In this case you need some mechanism for sharing data between the two plug-ins.</description>
		<content:encoded><![CDATA[<p>Where I think the data() method is useful is when you need to associate a value with a DOM element that needs to be publicly available. For example, you&#8217;re developing two companion plug-ins that need to work with each other. In this case you need some mechanism for sharing data between the two plug-ins.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ryan Townsend</title>
		<link>http://james.padolsey.com/javascript/its-just-an-api/comment-page-1/#comment-13889</link>
		<dc:creator>Ryan Townsend</dc:creator>
		<pubDate>Tue, 09 Jun 2009 12:06:07 +0000</pubDate>
		<guid isPermaLink="false">http://james.padolsey.com/?p=916#comment-13889</guid>
		<description>I&#039;m not sure if jQuery&#039;s data() method uses this, but HTML5 has &#039;data&#039; attributes, which are attributes named anything prefixed with &#039;data-&#039; and can be used at the developer&#039;s whim.

Typical use of them is data-remote=true for specifying AJAX usage, and data-method for hyperlinks that should run a command onclick instead of visiting their usual URL, thus promoting unobtrusive JavaScript.

I use this in my Prototype-based web apps (Rails 3 will include this functionality as standard):

&lt;pre lang=&quot;javascript&quot;&gt;
  Event.observe(window, &#039;dom:loaded&#039;, function() {
    $$(&#039;a[data-method], button[data-method]&#039;).each(function(el) {
      el.observe(&#039;click&#039;, run_data_method)
    })
    $$(&#039;form[data-remote]&#039;).each(function(el) {
      el.observe(&#039;submit&#039;, submit_via_ajax)
    })
  })

  var run_data_method = function(event) {
    // stop the event from running
    Event.stop(event)
    // get our event element
    el = event.element()
    // run our method
    eval(el.getAttribute(&#039;data-method&#039;))
  }

  /* ... */
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>I&#8217;m not sure if jQuery&#8217;s data() method uses this, but HTML5 has &#8216;data&#8217; attributes, which are attributes named anything prefixed with &#8216;data-&#8217; and can be used at the developer&#8217;s whim.</p>
<p>Typical use of them is data-remote=true for specifying AJAX usage, and data-method for hyperlinks that should run a command onclick instead of visiting their usual URL, thus promoting unobtrusive JavaScript.</p>
<p>I use this in my Prototype-based web apps (Rails 3 will include this functionality as standard):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  Event.<span class="me1">observe</span><span class="br0">&#40;</span>window<span class="sy0">,</span> <span class="st0">'dom:loaded'</span><span class="sy0">,</span> <span class="kw2">function</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    $$<span class="br0">&#40;</span><span class="st0">'a[data-method], button[data-method]'</span><span class="br0">&#41;</span>.<span class="me1">each</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span>el<span class="br0">&#41;</span> <span class="br0">&#123;</span>
      el.<span class="me1">observe</span><span class="br0">&#40;</span><span class="st0">'click'</span><span class="sy0">,</span> run_data_method<span class="br0">&#41;</span>
    <span class="br0">&#125;</span><span class="br0">&#41;</span>
    $$<span class="br0">&#40;</span><span class="st0">'form[data-remote]'</span><span class="br0">&#41;</span>.<span class="me1">each</span><span class="br0">&#40;</span><span class="kw2">function</span><span class="br0">&#40;</span>el<span class="br0">&#41;</span> <span class="br0">&#123;</span>
      el.<span class="me1">observe</span><span class="br0">&#40;</span><span class="st0">'submit'</span><span class="sy0">,</span> submit_via_ajax<span class="br0">&#41;</span>
    <span class="br0">&#125;</span><span class="br0">&#41;</span>
  <span class="br0">&#125;</span><span class="br0">&#41;</span>
&nbsp;
  <span class="kw2">var</span> run_data_method <span class="sy0">=</span> <span class="kw2">function</span><span class="br0">&#40;</span>event<span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="co1">// stop the event from running</span>
    Event.<span class="kw3">stop</span><span class="br0">&#40;</span>event<span class="br0">&#41;</span>
    <span class="co1">// get our event element</span>
    el <span class="sy0">=</span> event.<span class="me1">element</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
    <span class="co1">// run our method</span>
    <span class="kw1">eval</span><span class="br0">&#40;</span>el.<span class="me1">getAttribute</span><span class="br0">&#40;</span><span class="st0">'data-method'</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
  <span class="br0">&#125;</span>
&nbsp;
  <span class="coMULTI">/* ... */</span></pre></div></div>

]]></content:encoded>
	</item>
</channel>
</rss>

