Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #3945 (closed defect: fixed)

Opened 3 years ago

Last modified 2 years ago

Ajax.Updater doesn't work when asynchronous = false

Reported by: bigsmoke@gmail.com Assigned to: sam@conio.net
Priority: high Milestone:
Component: Prototype Version: 1.0.0
Severity: critical Keywords: AJAX updater
Cc:

Description

When making a call as in the following example, the AJAX call will be made, but 'person' won't be updated. If I set 'asynchronous' to 'true', it'll work. This won't help me, though, because I need to execute some code that works with the content that will be inserted in the 'person' div. Firing the AJAX call synchronously seems like the only way to make this happen.

new Ajax.Updater(
  'person', '/people/edit/15',
  {asynchronous: false, evalScripts: true}
);

Change History

03/10/06 18:09:47 changed by anonymous

I had the same problem but I did two Ajax.updaters back to back. First one asynchronous:true and the next one asynchronous:false. Everything else was the same. Not sure why this worked, but it did what I wanted synchronously. I also tried it the other way around false then true but this didn't work. Maybe this will help for now.

03/31/06 13:28:58 changed by anonymous

I use following hack for this:

var updater = new Ajax.Updater(
  'person', '/people/edit/15',
  {asynchronous: false, evalScripts: true}
);

updater.updateContent();

04/08/06 04:35:35 changed by pjwal@hotmail.com

I made the following modification to get synchronous calls to work in prototype. The code begins at line 647. The problem is that when the asynch flag is false, the onreadystatechange event does not fire. So, simply call the respondToReadyState event yourself after sending the request.

if (this.options.asynchronous) {
   this.transport.onreadystatechange = this.onStateChange.bind(this);
   setTimeout((function() {this.respondToReadyState(1)}).bind(this), 
} 

this.setRequestHeaders();

var body = this.options.postBody ? this.options.postBody : parameters;
this.transport.send(this.options.method == 'post' ? body : null);
/// BEGIN: FIX TO MAKE SYNCHRONOUS CALLS TO WORK
if (!this.options.asynchronous) {
   this.respondToReadyState(this.transport.readyState);
}
/// END: FIX TO MAKE SYNCHRONOUS CALLS TO WORK

06/26/06 14:59:56 changed by mr.daniel.brook@gmail.com

The problem here appears to be in the initialize method of Ajax.Updater where the containers property performs a $() on the container argument. Here's the relevant code corrected (this is the first block of code in the initialize method):

    this.containers = {                                                                                  
      success: container.success ? container.success : container,
      failure: container.failure ? container.failure :                                                  
        (container.success ? null : container)                                                            
    };

The problem with calling $() in the initialize method is that the Element.update method (which is called in the Ajax.Updater.updateContent method by default) already performs the $() itself. Why this breaks isn't apparent but the code above appears to fix the fault.

07/24/06 06:24:40 changed by petermichaux@gmail.com

  • keywords set to AJAX updater.

10/09/06 09:59:52 changed by mislav

  • status changed from new to closed.
  • resolution set to fixed.

This should now work, judging by the recent framework code. If not, take a look at #6366