Hi,

> My problem is that the +get_state+ method is not returning the
> <tt>state</tt> correctly.
> ....
> Am I doing something wrong?

Your `get_state` function, as coded, will always return "hidden".
That's because it _initiates_ an asynchronous request to the server
and then returns. Eventually the server will respond and your
onComplete handler will get called, but it doesn't matter, because
`get_state` has long since returned the string "hidden". The request
is *a*synchronous.

It's _possible_ to create synchronous requests (just change your
"asynchronous:true" to "asynchronous: false"), but you DON'T want to
do that. Making synchronous Ajax requests completely locks up the UI
in most browsers (including the most popular browser) throughout the
entire time of the request/response, which makes for bad user
experience. When I say "locks up the UI", I don't mean just your page,
but all pages displayed by that browser process. It's ugly. IE won't
even repaint.

Fixing this properly will probably involve redesigning your code.
Whenever you have logic that says "now I must go check some
information on the server," you _can't_ have that logic inline like
this:

   if (getInfoFromServer() == "some value") {
       doSomething();
   }
   else {
       doSomethingElse();
   }

...because talking to the server takes time, and you don't want your
code sitting there waiting for the server and locking up the UI.

Instead, you want to change the code to be event-based, involving
callbacks:

    getInfoFromServer(
        doSomething,
        doSomethingElse
    );

...where getInfoFromServer *starts* an asynchronous request, and then
when that request completes calls one of the two functions you've
passed in as arguments, depending on what the server response was.

This requires a "paradigm shift" in your thinking, but makes for
wonderfully responsive applications.

HTH,
--
T.J. Crowder
Independent Software Consultant
tj / crowder software / com
www.crowdersoftware.com


On Dec 1, 4:10 am, ekampp <eka...@gmail.com> wrote:
> Hi there.
>
> I have a prototype class that has these two methods:
>
>         hide_precollapsed: function(){
>                 if(this.get_state() != "visible"){
>                         this.target.hide();
>                 };
>         },
>
>         get_state: function(){
>                 var state = "hidden";
>                 new Ajax.Request(this.options.url, {
>                         asynchronous:true,
>                         method:'post',
>                         parameters:'?do=get&id='+this.trigger.id,
>                         onComplete: function(response){
>                                 state = response.responseText;
>                         }
>                 });
>
>                 return state;
>         }
>
> By logging the response from the requested url, and by alerting values
> for debugging I have determines that the correct values are beeing
> returned from the ajax request. And the targets and variables that is
> reffered does exist.
>
> My problem is that the +get_state+ method is not returning the
> <tt>state</tt> correctly. It is simply not getting to the
> +hide_precollapsed+ method. Well, that's not entirely true, if I add a
> "alert('...')" to the +get_state+ method, just before the return, the
> +hide_precollapsed+ will function properly.
>
>         get_state: function(){
>                 var state = "hidden";
>                 new Ajax.Request(this.options.url, {
>                         asynchronous:true,
>                         method:'post',
>                         parameters:'?do=get&id='+this.trigger.id,
>                         onComplete: function(response){
>                                 state = response.responseText;
>                         }
>                 });
>
>                 alert('...');
>                 return state;
>         }
>
> To me this makes no sense at all! Why is it that I need to alert
> something to the user in order to get the state to return correctly.
>
> Am I doing something wrong?
>
> Best regards
> Emil

--

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-scriptacul...@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