My script works like this.
First I''m calling a function named getRecordCount() that does an Ajax
request and updates one of my objects variables.
After that I''m trying to use that variable in another function, but
the value is null. The request haven''t finished before my other
function already trying to use the result of it.
So my question is, how do I first run function number one, and when
that function is finish, I call function number two?
Simplification of my code:
var myObject = {
//Default values
currentRecord: null,
initialize: function() {
//Call request and set value
this.getRecordCount()
//My other function
myOtherFunction();
},
getRecordCount: function() {
	var myAjax = new Ajax.Request(
			''getObjects.aspx'',
			{
				method: ''get'',
				parameters: '''',
				onSuccess: function(response) {
					myObject.recordCount = eval(''('' + response.responseText +
'')'');
				}
			});
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---
I would put your next function inside the Ajax.Request, as a  
callback. Then you are sure to have the value.
getRecordCount: function() {
	var myAjax = new Ajax.Request(
			''getObjects.aspx'',
			{
				method: ''get'',
				parameters: '''',
				onSuccess: function(response) {
					myObject.recordCount = eval(''('' + response.responseText +
'')'');
				},
				onComplete: function(){
					//do something with recordCount here
					//it''s guaranteed to be here at that point
				}
			});
}
Walter
On Feb 6, 2008, at 2:19 PM, mng0 wrote:
> getRecordCount: function() {
>
> 	var myAjax = new Ajax.Request(
> 			''getObjects.aspx'',
> 			{
> 				method: ''get'',
> 				parameters: '''',
> 				onSuccess: function(response) {
> 					myObject.recordCount = eval(''('' +
response.responseText + '')'');
> 				}
> 			});
> }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---
Thanks Walter, I may be naive now but is there another way to solve this problem? I think I''m getting better structure and overview if I just call it, and then when it''s done, I can continue call my other functions. After I''m getting the Value I''m calling like 7 more functions. But is this the only and most safe way, then I do it! :) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Anyways, I solved in your way, It work perfect! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Anyway, I solved it in your way, it works perfect! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Another way would be to use observer pattern.
...
onSuccess: function(t) {
  // fire global event passing evaluated JSON
  document.fire(''recordCount:received'', { recordCount:
t.responseText.evalJSON() });
}
...
// the function will now fire event once data is received
this.getRecordCount();
// subscribe myOtherFunction to this event
document.observe(''recordCount:received'', myOtherFunction)
...
// the data which was carried within event is available via "memo"
myOtherFunction(e) {
  e.memo.recordCount;
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---
If you''re using 1.6,
onSuccess: function(response) {
  myObject.recordCount = eval(''('' + response.responseText +
'')'');
},
is elegantly replaced by:
onSuccess: function(response) {
  myObject.recordCount = response.responseJSON;
},
Provided you''ve either set your content headers to application/json or
set the evalJSON option to ''force''.
Best,
Tobie
On Feb 6, 9:49 pm, kangax <kan...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Another way would be to use observer pattern.
>
> ...
> onSuccess: function(t) {
>   // fire global event passing evaluated JSON
>   document.fire(''recordCount:received'', { recordCount:
> t.responseText.evalJSON() });}
>
> ...
>
> // the function will now fire event once data is received
> this.getRecordCount();
>
> // subscribe myOtherFunction to this event
> document.observe(''recordCount:received'', myOtherFunction)
> ...
>
> // the data which was carried within event is available via
"memo"
> myOtherFunction(e) {
>   e.memo.recordCount;
>
> }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Spinoffs" group.
To post to this group, send email to
rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-spinoffs?hl=en
-~----------~----~----~----~------~----~------~--~---