That's a closure to save the 'callback' variable in it's own scope.
Actually it's not needed :)

The way that works is we create an anonymous function that will run
immediately, accepting as a parameter the callback name, and then
return a function which will be assigned to window[callback];

Like

myFunctionWithBeef = (function(x){
   //'beef' is available in here in the var 'x';
   return function(){...}; //this function will be stored in the
'myFunctionWithBeef' variable, and will have access to all vars in
this scope
})('beef');

Here is how it looks without it:

  self.timeout = function(){
    if (self[callback]){
      window[callback] = function(){};
      s.onTimeout(s.url);
      $('head script[src$='+s.url+']').remove();
      delete self[callback];
    };
};

The function is already declared in the scope where 'callback' is
available, so that closure is unnecessary. I think I left it that way
because it was originally inside a timeout, which runs in the global
scope.

One thing you must be aware of is that this script will not allow two
consecutive ajax calls with the same callback function, 'cause one
will overwrite the other.

cheers,
- ricardo

On 30 jan, 22:14, Stefano Corallo <stefan...@gmail.com> wrote:
> :) in effect i've investigated but i'm not a guru of js so i need some
> help ... thanks.
>
> last but not least :D can you explain this code:
>
> self[callback] = window[callback];
>   window[callback] = (function(callback){
>     return function(data){
>        (window[callback] = self[callback])(data);
>         delete self[callback];
>         clearTimeout(timer);
>    };
>   })(callback);
>
> On 30 Gen, 23:57, Ricardo Tomasi <ricardob...@gmail.com> wrote:
>
> > Ah, an oversight of mine. I forgot to add the parameter when rewriting
> > the callback.
>
> > Fixed as you can see athttp://jsbin.com/eliwu
>
> > Never use someone else's code without inspection ;)
>
> > On Jan 30, 4:34 pm, Stefano Corallo <stefan...@gmail.com> wrote:
>
> > > On 30 Gen, 19:25, Ricardo Tomasi <ricardob...@gmail.com> wrote:
>
> > > > We can't access the success function of the $.ajax call without
> > > > messing with jQuery source code, because the vars that name it are
> > > > inside the jQuery function scope.
>
> > > ok
>
> > > > In JSONP, the JSON object is passed as an argument to the callback
> > > > function. In the example I posted:
>
> > > > jsonFlickrFeed = function(data){ //do something with data };
>
> > > i've tried but data is always undefined,take a look:
>
> > >http://jsbin.com/ajimu/edit
>
> > > > On Jan 30, 7:28 am, Stefano Corallo <stefan...@gmail.com> wrote:
>
> > > > > On 28 Gen, 17:03, Ricardo Tomasi <ricardob...@gmail.com> wrote:
>
> > > > > > It isn't possible to have error/success callbacks for JSONP. You 
> > > > > > can't
> > > > > > cancel the request either. Once you add a <script> tag to the head,
> > > > > > the browser fires a GET request and it can't be interrupted - even 
> > > > > > if
> > > > > > you remove the script before the response comes, the script will 
> > > > > > still
> > > > > > run.
>
> > > > > ok. got it!
>
> > > > > > (the only event that fires on the script tag is 'onload', but it 
> > > > > > only
> > > > > > tells you if the content has successfully loaded, not the opposite.
> > > > > > There is no way to know if an error happened)
>
> > > > > > Also, you're misunderstanding the way JSONP works - there is no need
> > > > > > for a 'success' function, that is the callback function which will 
> > > > > > be
> > > > > > called when the response is loaded. Thats 'jsonFlickerFeed' in my
> > > > > > example.
>
> > > > > Ok i've understood how jsonp work, but there's something that i can't
> > > > > understand.
> > > > > I try to explain better i can (sorry for my bad english)
>
> > > > > 1)Using the $.ajax method, an example:
> > > > > $.ajax({
> > > > >                            type: methodType,
> > > > >                            url: theURL,
> > > > >                            data: params,
> > > > >                            dataType:"jsonp",
> > > > >                            success: function(data){
> > > > >                                         console.log("onClick 
> > > > > success");
> > > > >                                    }
> > > > >                             });
>
> > > > > in the jquery core is built a temporary jsonp function to handle the
> > > > > data returned by the server, if the succes function is defined the
> > > > > temporary function call the succe function and pass the returned
> > > > > data ... specifically at line 3314 of the jquery.1.3.1.js :
>
> > > > > // Handle JSONP-style loading
> > > > >                         window[ jsonp ] = function(tmp){
> > > > >                                 data = tmp;
> > > > >                                 success();
> > > > >                                 complete();
> > > > >                                 // Garbage collect
> > > > >                                 window[ jsonp ] = undefined;
> > > > >                                 try{ delete window[ jsonp ]; } 
> > > > > catch(e){}
> > > > >                                 if ( head )
> > > > >                                         head.removeChild( script );
> > > > >                         };
>
> > > > > the tmp is the data returned by the server.
>
> > > > > Now in your example you use the jsonFlickrFeed function but without
> > > > > handling the returned data from the server:
>
> > > > > jsonFlickrFeed = function(){ alert('flickr loaded'); };
> > > > > $.jsonp({
> > > > >   url: 'http://api.flickr.com/services/feeds/photos_public.gne?
> > > > > tags=hackdayindia&lang=en-us&format=json&callback=jsonFlickrFeed',
> > > > >   timeout: 10,
> > > > >   onTimeout: function(url){ console.error('jsonp script timed out:
> > > > > '+url) }
>
> > > > > });
>
> > > > > how to access the data returned from the server? How to modify the
> > > > > $.jsonp function to call the success function defined and pass in the
> > > > > data returned from the server ... this is sure possible case jquery in
> > > > > $.ajax call do it, but i don't know how :-/
>
> > > > > Many thanks, really!
>
> > > > > I hope i've been clear, sorry for my english!

Reply via email to