Hi George,

If I'm reading that right on a fairly quick skim, it looks like you've
already taken a stab at this and gotten very close:

> onSuccess  : function(transport){
>     this.RS = transport.responseText.evalJSON();
>     if (a==1) this._drawPage;
>   });},

The thing is that "this", within the onSuccess handler, won't be what
it is in the _getData call.  "this" is set by how a function is
called, rather than where and how it's defined.  The anonymous
function being used for the onSuccess handler will be called with
"this" being something else (I don't immediately recall what), rather
than it being a reference to an instance of your class.

This is easily fixed, though, using Function#bind[1]:

> onSuccess  : (function(transport){
>     this.RS = transport.responseText.evalJSON();
>     if (a==1) this._drawPage();
>   }).bind(this));},

[1] http://prototypejs.org/api/function/bind

I put parens around the function definition, and then put .bind(this)
after it.  I also put parens after the this._getData call to make it a
call to the function rather than a reference to the function object.

Details on the Function#bind page, an also here are some useful
articles:

http://proto-scripty.wikidot.com/prototype:how-to-hooking-events#BoundExample
http://blog.niftysnippets.org/2008/04/you-must-remember-this.html
http://www.alistapart.com/articles/getoutbindingsituations

(I'm rushing a bit this morning, apologies if I misread the question
or code and the above is off-base.)

HTH,
--
T.J. Crowder
tj / crowder software / com

On Dec 15, 5:07 am, George <george.bea...@googlemail.com> wrote:
> Hi Folks,
>
> I'm wondering if there is a way to get a function to wait until an
> external Ajax.Request has completed.  As always, I find an example is
> the best way to describe my issue:
>
> var buildPage = {};
> buildPage = Class.create();
> buildPage.prototype =
> {
> initialize: function() {
> this.RS = [];//hold json data for later use
>  ...
> this._getData(1);},
>
> _getData : function(a) {
> var url = (a==1) ? 'url1' : 'url2';
> new Ajax.Request (url), {method:'get',
> onSuccess  : function(transport){
>     this.RS = transport.responseText.evalJSON();
>     if (a==1) this._drawPage;
>   });},
>
> _drawPage : function () {
> //parse this.RS data with template
> ...},
>
> _refreshData : function () {
>  this.getData(2);
> //wait until _getData reports success then continue
>  this._drawPage();
>
> }
> };
>
> So this buildPage class reuses the _getData method calling a different
> URL depending on what is passed to it.  What I want the _refreshData
> method to do is call the _getData method, and only when it has
> successfully refreshed the RS array, call the _drawPage method.
>
> Is this possible? should I be doing it a different way?
>
> Many thanks
>
> George
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to