Marc
Marc

Reputation: 377

How to create a Prototype Ajax.Updater onComplete hook for all calls

Is there any chance to observe within the prototype framework if a dom element has been updated by Ajax.Updater?

Instead of writing a function in the onComplete parameter, I would like to observe that event for all Ajax.Updater calls. Any idea on how to do that?

Upvotes: 0

Views: 1677

Answers (2)

clockworkgeek
clockworkgeek

Reputation: 37700

There is Ajax.Responders which allows you to watch 'ajax events' (not really events in the javascript sense). The API says the first parameter passed to your callback is the requester object so you could check specifically if it is an Ajax.Updater and then check it's .container.success to find the element ID being updated. You can probably assume since it's an onComplete event then the referenced element is going to be changed.

Upvotes: 1

FK82
FK82

Reputation: 5075

I don't think you can go without an onComplete handler, unless you want to rewrite the prototype ajax API. However you do not have to directly pass a closure to the onComplete handler.

What you could do is create an empty object on top level scope within your javascript code:

 var updates = { } ;

Now you would set the onComplete parameter to contain a compact function that sets a property to the updates object by the name of the div you updated (+ date for multiple updates):

 function update() {

     var divName = arguments[0] ;
     var date = new Date() ;

         if( updates[divName] ) { //!! if there is an entry for that div fill date
                 updates[divName].push(date) ;
         } else { //!! else create a new array with the current date
                 updates[divName] = [ date ] ;
         }

 }

Now you may check for updates by checking the object for a property by the name of the div:

 /* code */
     if( updates[divName] !== null ) {
         isAlreadyUpdated() ;
     } else {
         isNotAlreadyUpdated() ;
     }
 /* more code */

Btw, I'm not excluding the possibility that there may be a method in the prototype API that will do something similar. I'm not that familiar with prototype.

If there is none however, I hope this will help!

FK

Upvotes: 0

Related Questions