Guldasta (A bouquet of flowers)

… because life is about varied experiences

  • Subscribe

  • Blog Stats

    • 37,599 hits
  • Disclaimer

    All which I express here are my independent views/opinions and are not necessarily influenced by any organization I work/correspond with.

Prototype.js lessons

Posted by imdad on September 13, 2008

Hey,
I repeated the same mistake .. so i chose to warn you.

Wrong Code:

coolFunction1 = function(){
  var data;
  new Ajax.Request('some_weird_url_json.php',{
    OnSuccess: funtion(transport){
      data = transport.responseJSON;
    }
  });
  display(data); // attempting to use the 'data' variable
}

What we wud expect is that the whatever value we have got back through the ajax request will be immediately available, WRONG, callback OnSuccess is only triggered ‘on-success’, so any attempt to use the latest ‘data’ should be inside this callback.

Correct Code:

coolFunction1 = function(){
  var data;
  new Ajax.Request('some_weird_url_json.php',{
    OnSuccess: funtion(transport){
      data = transport.responseJSON;
      display(data); // phew .. tht was the problem
    }
  });
}
Advertisements

2 Responses to “Prototype.js lessons”

  1. kangax said

    This is actually an invalid JavaScript.
    You probably meant:


    onSuccess: function(){ … }

  2. imdad said

    oh dear .. yes .. thnx for correcting me ..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: