Ah, I did misread the problem. Now I follow you, thanks for the
clarification.

There is indeed a better and simpler way to do this, which works with
*any*kind of asynchronous callback. You don't need to rely on having a
specific
object returned that you can stuff data into. Simply use a closure.

For example:

function jsonRequest( url, myVariable ) {
    $.getJSON( url, function( data ) {
        // here you have access to data and myVariable
    });
}

jsonRequest( 'test1.php', 'hello world' );
jsonRequest( 'test2.php', 'goodbye cruel world' );
jsonRequest( 'test3.php', 'off to join the circus' );

Each of those jsonRequest() calls has its own private 'myVariable' - no
globals needed.

Even for ordinary XHR, I think this is a much cleaner way to pass data into
the callback than messing with the XHR object, and the nice thing is you can
always use the same technique.

It's no different from how you might use a closure with setTimeout or any
other async function:

function delayedAlert( time, message ) {
    setTimeout( function() {
        alert( message );
    }, time );
}

delayedAlert( 1000, 'one second' );
delayedAlert( 10000, 'ten seconds' );

-Mike

On Sat, Sep 19, 2009 at 5:12 PM, Blixa <shulgisnotmyem...@gmail.com> wrote:

>
> Hi Mike,
>
> I think you might have misread my post and the code attached.
>
> I am quite aware of the fact that the callback function only runs upon
> a successful completion of the request and is run asynchronously. If
> you look at the code i've written you'll see that i am in fact
> _counting_ on that since i am declaring variable_from_caller in the
> line after the callback code as i know this code will be run before
> the callback code (which contains a reference to
> variable_from_caller).
>
> My question, again, was in regards to passing data TO the callback
> function and not FROM the callback function. In my scenario i have to
> consider the possibility of multiple getJSONs called and so multiple
> callbacks running side by side so that i cannot use global variables
> to store this data, which is specific to each of the callback. The way
> i described here uses the actual XMLHttpRequest object (by assigning
> $.getJSON to the variable new_json) and only works if i modify the
> jQuery code (like i mentioned in my original post).
> Is there another way of passing data TO the callback function that is
> not global, other than the way i've done it?
> Shouldn't getJSON _ALWAYS_ return an XMLHttpRequest object, as the
> documentation states?
>
>
>
>
> On Sep 20, 12:17 am, Michael Geary <m...@mg.to> wrote:
> > getJSON, like all Ajax and Ajax-style calls, is *asynchronous*. That's
> what
> > the A in Ajax stands for.
> >
> > Instead of trying to use a return value, you need to use the
> > getJSONcallback function (which you're already providing) to do
> > whatever you want
> > with the data.
> >
> > So your code might look something like:
> >
> > $.getJSON( url, function( data ) {
> >     // do stuff with data here, such as:
> >     callSomeOtherFunction( data );
> >
> > });
> >
> > In that callback function, you can store the data anywhere you want. You
> can
> > call other functions. You can do anything you need to.
> >
> > What you *can't* do is try to use the data immediately after
> > getJSONreturns. At that point, the data has not yet been downloaded.
> > You have to
> > use the callback function and call any other functions you need from
> there.
> >
> > -Mike
> >
> > On Sat, Sep 19, 2009 at 1:16 PM, Blixa <shulgisnotmyem...@gmail.com>
> wrote:
> >
> > > I am using getJSON to get results from a different domain and i wanted
> > > to get _some_ object back when calling getJSON so that i can insert
> > > some variables into the callback scope.
> > > Basically, what i wanted to do was this:
> >
> > > var new_json = $.getJSON(url, function(data) {
> > >                                                 alert
> > > (this.variable_from_caller);
> > >                                                });
> > > new_json.variable_from_caller = 'value';
> >
> > > The problem is that since url is on a different domain, the call to
> > > getJSON (which end up calling ajax) returns undefined and not an
> > > object that can be referenced (this is on line 3504 of the
> > > uncompressed jquery-1.3.2.js).
> >
> > > I've noticed that if i change undefined to s i can reference the
> > > object from outside and have the variable exist within 'this' inside
> > > the anonymous callback, although s is not an XMLHttpRequest.
> >
> > > Is this a bug? am i doing something wrong or twisted and there's a
> > > much easier way of accomplishing this in a way i am not aware of?
>

Reply via email to