开发者

jQuery ajax and javascript objects

i've used prototype before, and i'm trying to learn jquery now. The problem: I have an object that makes an ajax call, and i want the success callback to call a functio开发者_开发百科n within my object. The problem is that inside the callback function, "this" does not point to my original class.

Example:

function C(){
    this.loadData();
}
C.prototype.loadData = function(){
    $.ajax({
       url:"URL/",
       dataType:'json',
       success:this.dataRetreived
    });
}
C.prototype.dataRetreived = function(JSON){
    console.info(this);
    console.info(JSON);
}

Using Prototype i'd could simply use .bind(this), but jquery has a different way of doing things..


There's a "proxy" method in jQuery 1.4 that's kind-of like "bind" in Prototype or Functional:

  success: $.proxy(instanceOfC, C.prototype.dataRetrieved)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜