views:

34

answers:

2

I would to use a callback function in the same 'class' when the ajax request is successful. Here's the code

$.ajax({
   type: 'get',
   url: ajax_url,
   success: this.callback
})

this.callback = function() {

  // ERROR! this doesn't point to the right context!
  this.run_some_process();

}

Are there any built-in JavaScript constructs that can allow me to get or save the correct context, without having to resort to a custom delegate function?

+2  A: 

If I understand your question correctly.

var that = this;

$.ajax({
   type: 'get',
   url: ajax_url,
   success: that.callback
})

that.callback = function() {
  // ERROR! this doesn't point to the right context!
  that.run_some_process();

}
Anders
A: 

Have a look at $.ajax's 'context' option.

karim79