<?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 Changes</title>
  <id>tag:opensoul.org,2009:/2007/7/12/observers-for-ajax-callbacks/changes</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/changes.xml" rel="self" type="application/atom+xml"/>
  <link href="/2007/7/12/observers-for-ajax-callbacks" rel="alternate" type="text/html"/>
  <updated>2007-07-13T14:33:21Z</updated>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2007-07-13:332:4</id>
    <published>2007-07-12T21:21:00Z</published>
    <updated>2007-07-13T14:33:21Z</updated>
    <link href="http://opensoul.org/2007/7/12/observers-for-ajax-callbacks" rel="alternate" type="text/html"/>
    <title>Observers for Ajax callbacks</title>
<content type="html">&lt;p&gt;In my Javascript-fu adventures over the past week or so, I&#8217;ve consistently run into the same problem.  I want to do slight variations of an Ajax request on an individual basis. Here is a common method from my code: (Note: these examples use &lt;code&gt;Event.addBehavior&lt;/code&gt; from &lt;a href=&quot;http://www.danwebb.net/2006/9/3/low-pro-unobtrusive-scripting-for-prototype&quot;&gt;Dan Webb&#8217;s excellent lowpro library&lt;/a&gt; for registering events.)&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Event.addBehavior({
  // hijack any forms with the class &amp;quot;new&amp;quot; and submit them using Ajax
  'form.new:submit': function(event) {
    this.request({evalScripts: true,
      onLoading: function() { this.disable(); }.bind(this),
      onComlete: function() { this.enable(); }.bind(this)
    });
    Event.stop(event);
  }
});&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;But occasionally, I want a certain form to do something slightly different. For example, I have some forms that are hidden by default and I want to hide them again after they are submitted:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Event.addBehavior({
  'form.new.hidden:submit': function(event) {
    this.request({evalScripts: true,
      onLoading: function() { this.disable(); }.bind(this),
      onComlete: function() { this.enable(); }.bind(this),
      onSuccess: function() { new Effect.BlindUp(this); }.bind(this)
    });
    Event.stop(event);
  },
})&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The problems with this are, 1) there is a lot of duplication, and 2) if this form has the &#8220;new&#8221; class name, it&#8217;s going to end up with 2 event handlers registered, both making an Ajax request.&lt;/p&gt;


	&lt;p&gt;So what I want is event observers on &lt;code&gt;Form.request&lt;/code&gt; and &lt;a href=&quot;/2007/7/11/adding-dom-methods-with-prototype&quot;&gt;&lt;code&gt;Anchor.request&lt;/code&gt;&lt;/a&gt; for the Ajax request lifecycle, so I can call &lt;code&gt;form.request.observe('loading', function() { … })&lt;/code&gt;, and that will be invoked any time an Ajax request is made for that form:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Event.addBehavior({
  // submit form requests using ajax
  'form.new:submit': function(event) {
    this.request({evalScripts: true});
    Event.stop(event);
  },

  // register observers to disable and enable the form
  'form.new': function() {
    this.request.observe({
      'loading': function() { this.disable(); }.bind(this),
      'complete': function() { this.enable(); }.bind(this)
    });
  },

  // register an observer to hide the form on success
  'form.new.hidden': function() {
    this.request.observe('success', function() {
      new Effect.BlindUp(this);
    }.bind(this));
  }
})&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;And this is where I need your help.&lt;/p&gt;


	&lt;p&gt;The first problem is that we need some type of event notification framework. I have that part solved by using a &lt;a href=&quot;http://source.collectiveidea.com/public/rails/javascripts/object.event.js&quot;&gt;slightly modified version&lt;/a&gt; of &lt;a href=&quot;http://livepipe.net/projects/object_event/&quot;&gt;Ryan Johnson&#8217;s Object.Event library&lt;/a&gt;, which basically allows you to add event observers to any object, and then call them by calling &lt;code&gt;notify('eventname')&lt;/code&gt; within the object.&lt;/p&gt;


	&lt;p&gt;Next, I&#8217;ve made a modified version of &lt;a href=&quot;/2007/7/11/adding-dom-methods-with-prototype&quot;&gt;Anchor.reqeust method from my post yesterday&lt;/a&gt;, which calls &lt;code&gt;notify&lt;/code&gt; on each of the Ajax callbacks, and extends the request methods with the event notification methods:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;if (!window.Anchor) var Anchor = new Object();

Anchor.Methods = {
  request: function(anchor, options) {
    anchor = $(anchor)
    callbacks = {}
    $A(['loading', 'complete', 'exception', 'failure', 'success']).each(function(event) {
      callbacks['on' + event.capitalize()] = function() {
        anchor.notify.apply(anchor, arguments.unshift(event));
      };
    });
    options = Object.extend(Object.extend({
      method: 'get'
    }, callbacks), options || {});
    return new Ajax.Request(anchor.readAttribute('href'), options);
  }
}
Object.Event.extend(Anchor.Methods.request);
Element.addMethods('a', Anchor.Methods);&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This is where the second problem comes in.  While the event notification methods are added to &lt;code&gt;Anchor.Methods.request&lt;/code&gt;, they don&#8217;t get added to the anchor objects when the &lt;code&gt;request&lt;/code&gt; method does. They&#8217;re getting lost in Prototype&#8217;s &lt;code&gt;Element.extend&lt;/code&gt; method that adds the extensions to each element.&lt;/p&gt;


	&lt;p&gt;And as soon as I get that problem solved, I&#8217;m going to have another one: one instance of &lt;code&gt;Anchor.request&lt;/code&gt; will be shared amongst all the anchor objects, so registering an observer on one will register it for every Ajax request.  What I need then is for the event observer to keep track of the registered observers in each object, even though the methods are on the shared &lt;code&gt;register&lt;/code&gt; function.&lt;/p&gt;


	&lt;p&gt;I could move the event registration methods to the anchor instead of on the &lt;code&gt;request&lt;/code&gt; method, but that has problems of it&#8217;s own. Namely, each element already has an &lt;code&gt;observe&lt;/code&gt; method for the browser&#8217;s events, and I can&#8217;t come up with a better name than &lt;code&gt;observe&lt;/code&gt;.  Besides, I think the methods belong on the request object; they are specific to the Ajax request.&lt;/p&gt;


	&lt;p&gt;So, does anyone have any good ideas?  Is this a lame/unnecessary feature?&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2007-07-12:331:3</id>
    <published>2007-07-12T21:21:54Z</published>
    <updated>2007-07-12T21:36:55Z</updated>
    <link href="http://opensoul.org/2007/7/12/observers-for-ajax-callbacks" rel="alternate" type="text/html"/>
    <title>Observers for Ajax callbacks</title>
<content type="html">&lt;p&gt;In my Javascript-fu adventures over the past week or so, I&#8217;ve consistently run into the same problem.  I want to do slight variations of an Ajax request on an individual basis. Here is a common method from my code: (Note: these examples use &lt;code&gt;Event.addBehavior&lt;/code&gt; from &lt;a href=&quot;http://www.danwebb.net/2006/9/3/low-pro-unobtrusive-scripting-for-prototype&quot;&gt;Dan Webb&#8217;s excellent lowpro library&lt;/a&gt; for registering events.)&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Event.addBehavior({
  // hijack any forms with the class &amp;quot;new&amp;quot; and submit them using Ajax
  'form.new:submit': function(event) {
    this.request({evalScripts: true,
      onLoading: function() { this.disable(); }.bind(this),
      onComlete: function() { this.enable(); }.bind(this)
    });
    Event.stop(event);
  }
});&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;But occasionally, I want a certain form to do something slightly different. For example, I have some forms that are hidden by default and I want to hide them again after they are submitted:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Event.addBehavior({
  'form.new.hidden:submit': function(event) {
    this.request({evalScripts: true,
      onLoading: function() { this.disable(); }.bind(this),
      onComlete: function() { this.enable(); }.bind(this),
      onSuccess: function() { new Effect.BlindUp(this); }.bind(this)
    });
    Event.stop(event);
  },
})&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The problems with this are, 1) there is a lot of duplication, and 2) if this form has the &#8220;new&#8221; class name, it&#8217;s going to end up with 2 event handlers registered, both making an Ajax request.&lt;/p&gt;


	&lt;p&gt;So what I want is event observers on &lt;code&gt;Form.request&lt;/code&gt; and &lt;a href=&quot;/2007/7/11/adding-dom-methods-with-prototype&quot;&gt;&lt;code&gt;Anchor.request&lt;/code&gt;&lt;/a&gt; for the Ajax request lifecycle, so I can call &lt;code&gt;form.request.observe('loading', function() { … })&lt;/code&gt;, and that will be invoked any time an Ajax request is made for that form:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Event.addBehavior({
  // submit form requests using ajax
  'form.new:submit': function(event) {
    this.request({evalScripts: true});
    Event.stop(event);
  },

  // register observers to disable and enable the form
  'form.new': function() {
    this.request.observe({
      'loading': function() { this.disable(); }.bind(this),
      'complete': function() { this.enable(); }.bind(this)
    });
  },

  // register an observer to hide the form on success
  'form.new.hidden': function() {
    this.request.observe('success', function() {
      new Effect.BlindUp(this);
    }.bind(this));
  }
})&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;And this is where I need your help.&lt;/p&gt;


	&lt;p&gt;The first problem is that we need some type of event notification framework. I have that part solved by using a &lt;a href=&quot;http://source.collectiveidea.com/public/rails/javascripts/object.event.js&quot;&gt;slightly modified version&lt;/a&gt; of &lt;a href=&quot;http://livepipe.net/projects/object_event/&quot;&gt;Ryan Johnson&#8217;s Object.Event library&lt;/a&gt;, which basically allows you to add event observers to any object, and then call them by calling &lt;code&gt;notify('eventname')&lt;/code&gt; within the object.&lt;/p&gt;


	&lt;p&gt;Next, I&#8217;ve made a modified version of &lt;a href=&quot;/2007/7/11/adding-dom-methods-with-prototype&quot;&gt;Anchor.reqeust method from my post yesterday&lt;/a&gt;, which calls &lt;code&gt;notify&lt;/code&gt; on each of the Ajax callbacks, and extends the request methods with the event notification methods:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;if (!window.Anchor) var Anchor = new Object();

Anchor.Methods = {
  request: function(anchor, options) {
    anchor = $(anchor)
    callbacks = {}
    $A(['loading', 'complete', 'exception', 'failure', 'success']).each(function(event) {
      callbacks['on' + event.capitalize()] = function() {
        this.notify.apply(this, arguments.unshift(event));
      }.bind(this);
    }.bind(this));
    options = Object.extend(Object.extend({
      method: 'get'
    }, callbacks), options || {});
    return new Ajax.Request(anchor.readAttribute('href'), options);
  }
}
Object.Event.extend(Anchor.Methods.request);
Element.addMethods('a', Anchor.Methods);&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This is where the second problem comes in.  While the event notification methods are added to &lt;code&gt;Anchor.Methods.request&lt;/code&gt;, they don&#8217;t get added to the anchor objects when the &lt;code&gt;request&lt;/code&gt; method does. They&#8217;re getting lost in Prototype&#8217;s &lt;code&gt;Element.extend&lt;/code&gt; method that adds the extensions to each element.&lt;/p&gt;


	&lt;p&gt;And as soon as I get that problem solved, I&#8217;m going to have another one: one instance of &lt;code&gt;Anchor.request&lt;/code&gt; will be shared amongst all the anchor objects, so registering an observer on one will register it for every Ajax request.  What I need then is for the event observer to keep track of the registered observers in each object, even though the methods are on the shared &lt;code&gt;register&lt;/code&gt; function.&lt;/p&gt;


	&lt;p&gt;I could move the event registration methods to the anchor instead of on the &lt;code&gt;request&lt;/code&gt; method, but that has problems of it&#8217;s own. Namely, each element already has an &lt;code&gt;observe&lt;/code&gt; method for the browser&#8217;s events, and I can&#8217;t come up with a better name than &lt;code&gt;observe&lt;/code&gt;.  Besides, I think the methods belong on the request object; they are specific to the Ajax request.&lt;/p&gt;


	&lt;p&gt;So, does anyone have any good ideas?  Is this a lame/unnecessary feature?&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2007-07-12:330:2</id>
    <published>2007-07-12T21:54:21Z</published>
    <updated>2007-07-12T21:28:23Z</updated>
    <link href="http://opensoul.org/2007/7/12/observers-for-ajax-callbacks" rel="alternate" type="text/html"/>
    <title>Observers for Ajax callbacks</title>
<content type="html">&lt;p&gt;In my Javascript-fu adventures over the past week or so, I&#8217;ve consistently run into the same problem.  I want to do slight variations of an Ajax request on an individual basis. Here is a common method from my code: (Note: these examples use &lt;code&gt;Event.addBehavior&lt;/code&gt; from &lt;a href=&quot;http://www.danwebb.net/2006/9/3/low-pro-unobtrusive-scripting-for-prototype&quot;&gt;Dan Webb&#8217;s excellent lowpro library&lt;/a&gt; for registering events.)&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Event.addBehavior({
  // hijack any forms with the class &amp;quot;new&amp;quot; and submit them using Ajax
  'form.new:submit': function(event) {
    this.request({evalScripts: true,
      onLoading: function() { this.disable(); }.bind(this),
      onComlete: function() { this.enable(); }.bind(this)
    });
    Event.stop(event);
  }
});&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;But occasionally, I want a certain form to do something slightly different. For example, I have some forms that are hidden by default and I want to hide them again after they are submitted:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Event.addBehavior({
  'form.new.hidden:submit': function(event) {
    this.request({evalScripts: true,
      onLoading: function() { this.disable(); }.bind(this),
      onComlete: function() { this.enable(); }.bind(this),
      onSuccess: function() { new Effect.BlindUp(this); }.bind(this)
    });
    Event.stop(event);
  },
})&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The problems with this are, 1) there is a lot of duplication, and 2) if this form has the &#8220;new&#8221; class name, it&#8217;s going to end up with 2 event handlers registered, both making an Ajax request.&lt;/p&gt;


	&lt;p&gt;So what I want is event observers on &lt;code&gt;Form.request&lt;/code&gt; and &lt;a href=&quot;/2007/7/11/adding-dom-methods-with-prototype&quot;&gt;&lt;code&gt;Anchor.request&lt;/code&gt;&lt;/a&gt; for the Ajax request lifecycle, so I can call &lt;code&gt;form.request.observe('loading', function() { … })&lt;/code&gt;, and that will be invoked any time an Ajax request is made for that form:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Event.addBehavior({
  // submit form requests using ajax
  'form.new:submit': function(event) {
    this.request({evalScripts: true});
    Event.stop(event);
  },

  // register observers to disable and enable the form
  'form.new': function() {
    this.request.observe({
      'loading': function() { this.disable(); }.bind(this),
      'complete': function() { this.enable(); }.bind(this)
    });
  },

  // register an observer to hide the form on success
  'form.new.hidden': function() {
    this.request.observe('success', function() {
      new Effect.BlindUp(this);
    }.bind(this));
  }
})&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;And this is where I need your help.&lt;/p&gt;


	&lt;p&gt;The first problem is that we need some type of event notification framework. I have that part solved by using a &lt;a href=&quot;http://source.collectiveidea.com/public/rails/javascripts/object.event.js&quot;&gt;slightly modified version&lt;/a&gt; of &lt;a href=&quot;http://livepipe.net/projects/object_event/&quot;&gt;Ryan Johnson&#8217;s Object.Event library&lt;/a&gt;, which basically allows you to add event observers to any object, and then call them by calling &lt;code&gt;notify('eventname')&lt;/code&gt; within the object.&lt;/p&gt;


	&lt;p&gt;Next, I&#8217;ve made a modified version of &lt;a href=&quot;/2007/7/11/adding-dom-methods-with-prototype&quot;&gt;Anchor.reqeust method from my post yesterday&lt;/a&gt;, which calls &lt;code&gt;notify&lt;/code&gt; on each of the Ajax callbacks, and extends the request methods with the event notification methods:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;if (!window.Anchor) var Anchor = new Object();

Anchor.Methods = {
  request: function(anchor, options) {
    anchor = $(anchor)
    callbacks = {}
    $A(['loading', 'complete', 'exception', 'failure', 'success']).each(function(event) {
      callbacks['on' + event.capitalize()] = function() {
        this.notify.apply(this, arguments.unshift(event));
      }.bind(this);
    }.bind(this));
    options = Object.extend(Object.extend({
      method: 'get'
    }, callbacks), options || {});
    return new Ajax.Request(anchor.readAttribute('href'), options);
  }
}
Object.Event.extend(Anchor.Methods.request);
Element.addMethods('a', Anchor.Methods);&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This is where the second problem comes in.  While the event notification methods are added to &lt;code&gt;Anchor.Methods.request&lt;/code&gt;, they don&#8217;t get added to the anchor objects when the &lt;code&gt;request&lt;/code&gt; method does. They&#8217;re getting lost in Prototype&#8217;s &lt;code&gt;Element.extend&lt;/code&gt; method that adds the extensions to each element.&lt;/p&gt;


	&lt;p&gt;And as soon as I get that problem solved, I&#8217;m going to have another one: one instance of &lt;code&gt;Anchor.request&lt;/code&gt; will be shared amongst all the anchor objects, so registering an observer on one will register it for every Ajax request.  What I need then is for the event observer to keep track of the registered observers in each object, even though the methods are on the shared &lt;code&gt;register&lt;/code&gt; function.&lt;/p&gt;


	&lt;p&gt;I could move the event registration methods to the anchor instead of on the &lt;code&gt;request&lt;/code&gt; method, but that has problems of it&#8217;s own. Namely, each element already has an &lt;code&gt;observe&lt;/code&gt; method for the browser&#8217;s events, and I can&#8217;t come up with a better name than &lt;code&gt;observe&lt;/code&gt;.  Besides, I think the methods belong on the request object; they are specific to the Ajax request.&lt;/p&gt;


	&lt;p&gt;So, does anyone have any good ideas?  Is this a lame/unnecessary feature?&lt;/p&gt;</content>  </entry>
  <entry xml:base="http://opensoul.org/">
    <author>
      <name>brandon</name>
    </author>
    <id>tag:opensoul.org,2007-07-12:329:1</id>
    <updated>2007-07-12T21:21:53Z</updated>
    <link href="http://opensoul.org/2009/3/10/observers-for-ajax-callbacks" rel="alternate" type="text/html"/>
    <title>Observers for Ajax callbacks</title>
<content type="html">&lt;p&gt;In my Javascript-fu adventures over the past week or so, I&#8217;ve consistently run into the same problem.  I want to do slight variations of an Ajax request on an individual basis. Here is a common method from my code: (Note: these examples use &lt;code&gt;Event.addBehavior&lt;/code&gt; from &lt;a href=&quot;http://www.danwebb.net/2006/9/3/low-pro-unobtrusive-scripting-for-prototype&quot;&gt;Dan Webb&#8217;s excellent lowpro library&lt;/a&gt; for registering events.)&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Event.addBehavior({
  // hijacks any forms with the class &amp;quot;new&amp;quot; and submit them using Ajax
  'form.new:submit': function(event) {
    this.request({evalScripts: true,
      onLoading: function() { this.disable(); }.bind(this),
      onComlete: function() { this.enable(); }.bind(this)
    });
    Event.stop(event);
  }
});&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;But occasionally, I want a certain form to do something slightly different. For example, I have some forms that are hidden by default and I want to hide them again after they are submitted:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Event.addBehavior({
  'form.new.hidden:submit': function(event) {
    this.request({evalScripts: true,
      onLoading: function() { this.disable(); }.bind(this),
      onComlete: function() { this.enable(); }.bind(this),
      onSuccess: function() { new Effect.BlindUp(this); }.bind(this)
    });
    Event.stop(event);
  },
})&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The problems with this are, 1) there is a lot of duplication, and 2) if this form has the &#8220;new&#8221; class name, it&#8217;s going to end up with 2 event handlers registered, both making an Ajax request.&lt;/p&gt;


	&lt;p&gt;So what I want is event observers on &lt;code&gt;Form.request&lt;/code&gt; and &lt;a href=&quot;/2007/7/11/adding-dom-methods-with-prototype&quot;&gt;&lt;code&gt;Anchor.request&lt;/code&gt;&lt;/a&gt; for the Ajax request lifecycle, so I can call &lt;code&gt;form.request.observe('loading', function() { … })&lt;/code&gt;, and that will be invoked any time an Ajax request is made for that form:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;Event.addBehavior({
  // submit form requests using ajax
  'form.new:submit': function(event) {
    this.request({evalScripts: true});
    Event.stop(event);
  },

  // register observers to disable and enable the form
  'form.new': function() {
    this.request.observe('loading', function() {
      this.disable();
    }.bind(this));
    this.request.observe('complete', function() {
      this.enable();
    }.bind(this));
  },

  // register an observer to hide the form on success
  'form.new.hidden': function() {
    this.request.observe('success', function() {
      new Effect.BlindUp(this);
    }.bind(this));
  }
})&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;And this is where I need your help.&lt;/p&gt;


	&lt;p&gt;The first problem is that we need some type of event notification framework. I have that part solved by using a &lt;a href=&quot;http://source.collectiveidea.com/public/rails/javascripts/object.event.js&quot;&gt;slightly modified version&lt;/a&gt; of &lt;a href=&quot;http://livepipe.net/projects/object_event/&quot;&gt;Ryan Johnson&#8217;s Object.Event library&lt;/a&gt;, which basically allows you to add event observers to any object, and then you can call them by calling &lt;code&gt;notify()&lt;/code&gt; within the object.&lt;/p&gt;


	&lt;p&gt;Next, I&#8217;ve made a modified version of my &lt;a href=&quot;/2007/7/11/adding-dom-methods-with-prototype&quot;&gt;Anchor.reqeust method from yesterday&lt;/a&gt;, which calls notify on each of the Ajax callbacks, and extends the request methods with the event notification methods:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;javascript&quot;&gt;if (!window.Anchor) var Anchor = new Object();

Anchor.Methods = {
  request: function(anchor, options) {
    anchor = $(anchor)
    callbacks = {}
    $A(['loading', 'complete', 'exception', 'failure', 'success']).each(function(event) {
      callbacks['on' + event.capitalize()] = function() {
        this.notify.apply(this, arguments.unshift(event));
      }.bind(this);
    }.bind(this));
    options = Object.extend(Object.extend({
      method: 'get'
    }, callbacks), options || {});
    return new Ajax.Request(anchor.readAttribute('href'), options);
  }
}
Object.Event.extend(Anchor.Methods.request);
Element.addMethods('a', Anchor.Methods);&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;This is where the second problem comes in.  While the event notification methods are added to &lt;code&gt;Anchor.Methods.request&lt;/code&gt;, they don&#8217;t get added to the anchor objects when the &lt;code&gt;request&lt;/code&gt; method does. They&#8217;re getting lost in Prototype&#8217;s &lt;code&gt;Element.extend&lt;/code&gt; method that adds the extensions to each element.&lt;/p&gt;


	&lt;p&gt;And as soon as I get that problem solved, I&#8217;m going to have another one: one instance of &lt;code&gt;Anchor.request&lt;/code&gt; will be shared amongst all the anchor objects, so registering an observer on one will register it for every Ajax request.  What I need then is for the event observer to keep track of the registered observers in each object, even though the methods are on the shared &lt;code&gt;register&lt;/code&gt; function.&lt;/p&gt;


	&lt;p&gt;I could move the event registration methods to the anchor instead of on the &lt;code&gt;request&lt;/code&gt; method, but that has problems of it&#8217;s own. Namely, each element already has an &lt;code&gt;observe&lt;/code&gt; method for the browser&#8217;s events, and I can&#8217;t come up with a better name than &lt;code&gt;observe&lt;/code&gt;.  Besides, I think the methods belong on the request object; they are specific to the Ajax request.&lt;/p&gt;


	&lt;p&gt;So, does anyone have any good ideas?  Is this a lame/unnecessary feature?&lt;/p&gt;</content>  </entry>
</feed>
