Hi TJ Many thanks for your response.
Actualy I'm not having any problem with execution scope and have managed to grasp a fairly good understanding over the past few weeks. In my constant attempts to re-use code, I'm trying to revisit a method that refreshes a dataset with an ajax call. The problem that I'm running into though is that I'm not able to determine when the data has been refreshed successfully from outside the method. This results in my calling function charging on and refreshing my page with out of date data. So my question is - is there some kind of global status I can access to tell whether or not the Ajax call has successfully completed. All the best George On Dec 15, 5:56 pm, "T.J. Crowder" <[email protected]> wrote: > 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#Boun...http://blog.niftysnippets.org/2008/04/you-must-remember-this.htmlhttp://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 <[email protected]> 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 [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~----------~----~----~----~------~----~------~--~---
