admin管理员组文章数量:1287867
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.
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. Can I somehow get jquery to pass a reference to 'this' store in the callback function?
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?
Another way for me could be, to send the store id also in the requests which will e back in the response. 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. A different request URL will be used for two different stores even though they have the same query parameters.
Is there any other way to achieve what I want?
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.
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. Can I somehow get jquery to pass a reference to 'this' store in the callback function?
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?
Another way for me could be, to send the store id also in the requests which will e back in the response. 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. A different request URL will be used for two different stores even though they have the same query parameters.
Is there any other way to achieve what I want?
Share Improve this question asked Jan 9, 2009 at 8:12 NileshNilesh4 Answers
Reset to default 5You 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; this.queryparam = 'blah'; 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. :-)
Thanks Marc and ricardoe !
I wrote this plugin, I think it will be useful
http://code.google./p/jquerycallback/
版权声明:本文标题:javascript - How to get jQuery to pass custom arguments to asynchronous AJAX callback functions? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741318982a2372090.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论