<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
  <title>opensoul.org - Observers for Ajax callbacks Comments</title>
  <id>tag:opensoul.org,2009:/2007/7/12/observers-for-ajax-callbacks/comments</id>
  <generator uri="http://mephistoblog.com" version="0.8.0">Mephisto Drax</generator>
  <link href="http://opensoul.org/2007/7/12/observers-for-ajax-callbacks/comments.xml" rel="self" type="application/atom+xml"/>
  <link href="/2007/7/12/observers-for-ajax-callbacks" rel="alternate" type="text/html"/>
  <updated>2007-07-13T12:24:34Z</updated>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Brandon</name>
    </author>
    <id>tag:opensoul.org,2007-07-12:3203:3220</id>
    <published>2007-07-13T12:24:34Z</published>
    <updated>2007-07-13T12:24:34Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/7/12/observers-for-ajax-callbacks" rel="alternate" type="text/html"/>
    <title>Comment on 'Observers for Ajax callbacks' by Brandon</title>
<content type="html">&lt;p&gt;John,&lt;/p&gt;


	&lt;p&gt;Hmm, good thinking.  I don&#8217;t know why I didn&#8217;t think about the idea of having an external &#8220;event registry&#8221;.  I was thinking that it needed to be on the actual object.  This solves the last problem of all the objects sharing an instance of &lt;code&gt;request&lt;/code&gt;.  The &lt;code&gt;request&lt;/code&gt; method could just call:&lt;/p&gt;


&lt;pre&gt;
Ajax.Responders.dispatch(this, 'onCallback', …);
&lt;/pre&gt;

	&lt;p&gt;And that could look up responders registered with an object.&lt;/p&gt;


	&lt;p&gt;Wheels are turning again&#8230;thanks John!&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>John Nunemaker</name>
    </author>
    <id>tag:opensoul.org,2007-07-12:3203:3219</id>
    <published>2007-07-13T12:10:40Z</published>
    <updated>2007-07-13T12:10:40Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/7/12/observers-for-ajax-callbacks" rel="alternate" type="text/html"/>
    <title>Comment on 'Observers for Ajax callbacks' by John Nunemaker</title>
<content type="html">&lt;blockquote&gt;
		&lt;p&gt;The problem with responders is that they don’t know anything about the element that triggered the request. Usually, when I want to add behavior, I want the behavior to act on the element, and not the request.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;Yep, understood. I wasn&#8217;t clear on that. What I meant was using that as a starting point, you could create something that allowed registering responders for particular elements.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Brandon</name>
    </author>
    <id>tag:opensoul.org,2007-07-12:3203:3218</id>
    <published>2007-07-13T12:06:47Z</published>
    <updated>2007-07-13T12:06:47Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/7/12/observers-for-ajax-callbacks" rel="alternate" type="text/html"/>
    <title>Comment on 'Observers for Ajax callbacks' by Brandon</title>
<content type="html">&lt;p&gt;John,&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;what about using classes on the a or form&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;Yeah, I thought about that, but I think it&#8217;s a better design to have them off the request methods, since they&#8217;re specific to the Ajax request. But, if all else fails, this will work.&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;Have you looked through the Ajax.Responders code in prototype? You could create something along those lines that only adds the responders to particular elements.&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;The problem with responders is that they don&#8217;t know anything about the element that triggered the request.  Usually, when I want to add behavior, I want the behavior to act on the element, and not the request.&lt;/p&gt;


	&lt;blockquote&gt;
		&lt;p&gt;This is a sweet problem. I wish I had time to prototype and try some solutions. That’s why I keep commenting. :)&lt;/p&gt;
	&lt;/blockquote&gt;


	&lt;p&gt;By all means, keep commenting.  I&#8217;ve been thinking about this and trying different things for a couple days and haven&#8217;t come up with a clean solution.  All I know is it is something I&#8217;m struggling with in every unobtrusive Ajax app that I&#8217;ve worked on lately.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>John Nunemaker</name>
    </author>
    <id>tag:opensoul.org,2007-07-12:3203:3217</id>
    <published>2007-07-13T11:48:34Z</published>
    <updated>2007-07-13T11:48:34Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/7/12/observers-for-ajax-callbacks" rel="alternate" type="text/html"/>
    <title>Comment on 'Observers for Ajax callbacks' by John Nunemaker</title>
<content type="html">&lt;p&gt;Have you looked through the Ajax.Responders code in prototype? You could create something along those lines that only adds the responders to particular elements.&lt;/p&gt;


	&lt;p&gt;This is a sweet problem. I wish I had time to prototype and try some solutions. That&#8217;s why I keep commenting. :)&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>John Nunemaker</name>
    </author>
    <id>tag:opensoul.org,2007-07-12:3203:3216</id>
    <published>2007-07-13T11:44:10Z</published>
    <updated>2007-07-13T11:44:10Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/7/12/observers-for-ajax-callbacks" rel="alternate" type="text/html"/>
    <title>Comment on 'Observers for Ajax callbacks' by John Nunemaker</title>
<content type="html">&lt;p&gt;I don&#8217;t know if I have totally thought this through and it is 7:30AM but what about using classes on the a or form. So on loading you want to call this.disable() so you classify it loading_disable. You could loop through the classes on an element before making the request:&lt;/p&gt;


	&lt;p&gt;callbacks       = {}
valid_callbacks = $A([&#8216;loading&#8217;, &#8216;complete&#8217;, &#8216;exception&#8217;, &#8216;failure&#8217;, &#8216;success&#8217;]);&lt;/p&gt;


	&lt;p&gt;this.classNames.each(function(class) {
    var class_split = class.split(&#8217;_&#8217;);
  var possible_callback = class_split.first();
    if (valid_callbacks.include(possible_callback)) {
        callbacks[&#8216;on&#8217; + possible_callback.capitalize()] = function() {
        this[class_split.shift.join(&#8217;-&#8217;).camelize()];
      }.bind(this);
    }
}.bind(this));&lt;/p&gt;


	&lt;p&gt;So if you had loading_disable, this.disable would be called and if you had loading_disable_my_poo, this.disableMyPoo would be called. Granted I&#8217;m sure my code is off and you also might want to join all the loading events together in one function and the same with all the other callbacks to allow for using loading_thing and loading_another and not have them overwrite each other but I think it could work.&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Brandon</name>
    </author>
    <id>tag:opensoul.org,2007-07-12:3203:3214</id>
    <published>2007-07-13T07:27:04Z</published>
    <updated>2007-07-13T07:27:04Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/7/12/observers-for-ajax-callbacks" rel="alternate" type="text/html"/>
    <title>Comment on 'Observers for Ajax callbacks' by Brandon</title>
<content type="html">&lt;p&gt;Matt,&lt;/p&gt;


	&lt;p&gt;That would work in some situations, and is in fact what I&#8217;m doing for the time being.  But it leads to a ton of duplication.  In several apps I&#8217;ve found myself using the same patterns, and duplicating the same code across all my rjs templates (helpers help with the duplication).&lt;/p&gt;


	&lt;p&gt;But the most common situation that I&#8217;m running into is wanting to change the &lt;code&gt;onLoading&lt;/code&gt; behavior, which obviously can&#8217;t be done through the response.  For some forms I only want it to change the button to &#8220;saving&#8221;, some forms I want it to gray out the element that is being updated, etc.&lt;/p&gt;


	&lt;p&gt;Does that make sense?  Am I making this more difficult than it needs to be?&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>Matt Jones</name>
    </author>
    <id>tag:opensoul.org,2007-07-12:3203:3209</id>
    <published>2007-07-12T23:42:31Z</published>
    <updated>2007-07-12T23:42:31Z</updated>
    <category term="Code"/>
    <link href="http://opensoul.org/2007/7/12/observers-for-ajax-callbacks" rel="alternate" type="text/html"/>
    <title>Comment on 'Observers for Ajax callbacks' by Matt Jones</title>
<content type="html">&lt;p&gt;Are you making calls to an external resource (that you
don&#8217;t have control over)? If not, then this is usually done
with a &lt;span class=&quot;caps&quot;&gt;JSON&lt;/span&gt; reply (for example, using &lt;span class=&quot;caps&quot;&gt;RJS&lt;/span&gt; in Rails).&lt;/p&gt;


	&lt;p&gt;Essentially, the server reply is responsible for hiding the 
form (using the Prototype effect).&lt;/p&gt;


	&lt;p&gt;Maybe I&#8217;m missing the point &#8211; would this work?&lt;/p&gt;</content>  </entry>
</feed>
