[jQuery] Re: How to bind data to the ajax callback function?

2007-10-15 Thread arphenlin

Wizzud,
Thanks for your help. I can understand the problem is the scope of
variables.
My idea is to transfer the userData to $.get() and then I can get it
back in the callback function without considering the variable scope
problem. Isn't it more intuitive?

On Oct 16, 1:24 am, Wizzud <[EMAIL PROTECTED]> wrote:
> It's not ambiguous at all.
> Your problem is not with the $.get() but with the code around it.
> $.get() does exactly what it is intended to do - it fetches a single
> 'page' (of some sort) and provides you with the mechanism for handling
> that page when successfully retrieved.
> You are trying to handle multiple $.get()s in a single loop, with each
> retrieved 'page' (presumably) being handled differently, and in the
> process of doing so you're getting the scope of some variables mixed
> up. That's all.
>
> If you really want to minimise it, and you really only have a loop of
> 2, then ...
>
> var url='http://foo.bar/?param=';
> $.each([0,1],function(i,j){
>     $.get(url+j, function(html){
> alert(j);
>   });
>   });
>
> On Oct 14, 2:10 pm, arphenlin <[EMAIL PROTECTED]> wrote:
>
> > It's so ambagious. I really hope jQuery can provide a *userData*
> > parameter like this:
> > jQuery.get( url, [data], [callback], [userData] )
>
> > Then I can achieve my goal with this way:
> > for(var i=0; i<2; i++){
> > $.get(url+i, function(html, userData){
> > doit(html, userData['tag']); // userData was bound to ajax
> > callback
> > }, {'tag': i});
>
> > }
> > Wizzud wrote:
> > > var url='http://foo.bar/?param=';
> > > for(var i=0; i<2; i++){
> > > submitAjax(i);
> > > }
> > > function submitAjax(i){
> > > $.get(url+i, function(html){
> > > doit(html, i);
> > > });
> > > }
> > > function doit(html, tag){
> > > alert(tag);
> > > }
>
> > > On Oct 13, 11:04 am, arphenlin <[EMAIL PROTECTED]> wrote:
> > > > Below is an example to use jQuery.get() to get some html data.
> > > > I expect that "0", "1" (or "1", "0") are displayed, however, it
> > > > displayed "2", "2".
> > > > How can I do?
>
> > > > var url='http://foo.bar/?param=';
> > > > for(var i=0; i<2; i++){
> > > > $.get(url+i, function(html){
> > > > doit(html, i); // bind 'i' to the callback function
> > > > });
>
> > > > }
>
> > > > function doit(html, tag){
> > > > alert(tag);
>
> > > > }



[jQuery] Re: How to bind data to the ajax callback function?

2007-10-15 Thread arphenlin

It's so ambagious. I really hope jQuery can provide a *userData*
parameter like this:
jQuery.get( url, [data], [callback], [userData] )

Then I can achieve my goal with this way:
for(var i=0; i<2; i++){
$.get(url+i, function(html, userData){
doit(html, userData['tag']); // userData was bound to ajax
callback
}, {'tag': i});
}


Wizzud wrote:
> var url='http://foo.bar/?param=';
> for(var i=0; i<2; i++){
> submitAjax(i);
> }
> function submitAjax(i){
> $.get(url+i, function(html){
> doit(html, i);
> });
> }
> function doit(html, tag){
> alert(tag);
> }
>
> On Oct 13, 11:04 am, arphenlin <[EMAIL PROTECTED]> wrote:
> > Below is an example to use jQuery.get() to get some html data.
> > I expect that "0", "1" (or "1", "0") are displayed, however, it
> > displayed "2", "2".
> > How can I do?
> >
> > var url='http://foo.bar/?param=';
> > for(var i=0; i<2; i++){
> > $.get(url+i, function(html){
> > doit(html, i); // bind 'i' to the callback function
> > });
> >
> > }
> >
> > function doit(html, tag){
> > alert(tag);
> >
> > }



[jQuery] How to bind data to the ajax callback function?

2007-10-13 Thread arphenlin

Below is an example to use jQuery.get() to get some html data.
I expect that "0", "1" (or "1", "0") are displayed, however, it
displayed "2", "2".
How can I do?

var url='http://foo.bar/?param=';
for(var i=0; i<2; i++){
$.get(url+i, function(html){
doit(html, i); // bind 'i' to the callback function
});
}

function doit(html, tag){
alert(tag);
}