简体   繁体   English

如何让jQuery将自定义参数传递给异步AJAX回调函数?

[英]How to get jQuery to pass custom arguments to asynchronous AJAX callback functions?

My page deals with many "Store" objects, each of them has a field called 'data'. 我的页面处理许多“商店”对象,每个对象都有一个名为“数据”的字段。 However, this data is fetched via AJAX requests which may be parallely going on. 但是,这些数据是通过AJAX请求获取的,这些请求可以并行进行。

function Store(id){
    this.id = id;
    this.queryparam = 'blah';
    this.items = null;
}

Store.prototype.fetch = function(){
    $.get("/get_items",{q:this.quaryparam},function(data,status){

      // how to store the received data in this particular store object? Being
      // a callback function, I don't have a reference to this object as 'this'

       // this.data = data; //will not work
    });
}

In the callback function I tried defining a default parameter to the calling objects as following: 在回调函数中,我尝试为调用对象定义一个默认参数,如下所示:

$.get("/get_items",{q:this.quaryparam},function(data,status, ref = this) ...

But turns out that javascript does not support default argument values like this. 但事实证明,javascript不支持这样的默认参数值。 Can I somehow get jquery to pass a reference to 'this' store in the callback function? 我可以以某种方式让jquery在回调函数中传递对'this'存储的引用吗?

I thought of a couple of other approaches but none of them work: 我想到了其他几种方法,但没有一种方法可行:

I could set the store data using synchronous requests but then thats not the point of AJAX, is it? 我可以使用同步请求设置商店数据,但那不是AJAX的重点,是吗?

Another way for me could be, to send the store id also in the requests which will come back in the response. 对我来说另一种方法是,也可以在响应中返回的请求中发送商店ID。 For eg: 例如:

// in Store.fetch()
$.get("/get_items",{q:this.quaryparam,id:this.id},function(responsetext,status){
    var response = eval(responsetext);
    stores[response.id].data = response.data;
});

I do not like this approach because this pollutes the response just because the client-side code was unable to keep track of which request was sent by which object. 我不喜欢这种方法,因为这会污染响应只是因为客户端代码无法跟踪哪个对象发送了哪个请求。

Moreover, since store.id is client-specific, it will also mess up caching at the server. 此外,由于store.id是特定于客户端的,因此它也会破坏服务器上的缓存。 A different request URL will be used for two different stores even though they have the same query parameters. 不同的请求URL将用于两个不同的存储,即使它们具有相同的查询参数。

Is there any other way to achieve what I want? 有没有其他方法可以实现我想要的?

You should be able to use closure: 你应该能够使用闭包:

var tmp = this;
$.get("/get_items",{q:this.quaryparam},function(data,status){
    tmp.data = data;
});

Is that what you mean? 你是这个意思吗?

function Store(id){ this.id = id; function Store(id){this.id = id; this.queryparam = 'blah'; this.queryparam ='blah'; this.items = null; this.items = null; } }

Store.prototype.fetch = function(){
    var that = this;
    $.get("/get_items",{q:this.quaryparam},function(response){
        that.callback(response)
    });
}

Store.prototype.callback = function(response){
    // and here you can use
   this.items = response // just example
}

Seems to be working, although I don't understand how the variable 'tmp' is accessible inside the anonymous function. 似乎工作,虽然我不明白如何在匿名函数内访问变量'tmp'。 :-) :-)

Thanks Marc and ricardoe ! 谢谢Marc和ricardoe!

I wrote this plugin, I think it will be useful 我写了这个插件,我觉得它会很有用

http://code.google.com/p/jquerycallback/ http://code.google.com/p/jquerycallback/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM