Some questions about Ajax.Updater


#1

Hi everyone,

I’ve been thinking of actually getting rid of the Rails javascript tag
helpers, and making calls to the prototype library directly from the
javascript functions that I made, however there are some bits that I
don’t quite seem to get…

Does this method block until it completes? As in, suppose I use it in a
block of javascript, will the code that’s right after it execute only
after the Ajax call has completed?

If I’m using it to evaluate javascript expressions returned from the
server, what context are these expressions executed in? For instance I’m
actually planning to use the method in a class method, and hence if I
evaluate the returned javascript express “this.foobar”, will “this” be
the object from which the ajax updater is called or… nothing?

Thanks for reading.


#2

Hey there,

Does this method block until it completes? As in, suppose I use it in a
block of javascript, will the code that’s right after it execute only
after the Ajax call has completed?

No, the AJAX call is asynchronous, that’s what the A stands for ;). If
it was synchronous the browser would be unresponsive until the request
had completed (which isn’t usually what you want, but I guess it could
be). If you want a synchronous request you can pass { asynchronous:
false } in the Ajax.Updater options.

If I’m using it to evaluate javascript expressions returned from the
server, what context are these expressions executed in? For instance I’m
actually planning to use the method in a class method, and hence if I
evaluate the returned javascript express “this.foobar”, will “this” be
the object from which the ajax updater is called or… nothing?

It can execute in the context of the object. Look at Ajax.Autocompleter
in effects.js for an example. I think you would want something like:

performRequest: function() {
this.chocolate = “mmmmmm”;
new Ajax.Updater(‘my_div’, ‘my_url’, { onComplete:
this.onComplete.bind(this) });
},

onComplete: function() {
alert("Here I am, chocolate is " + this.chocolate);
}

I haven’t tested it, but I think you want something like that…

Thanks for reading.

No prob. -Jonny.