Passing variables to callback functions

Posted by mickyjtwin on Stack Overflow See other posts from Stack Overflow or by mickyjtwin
Published on 2010-05-24T06:28:09Z Indexed on 2010/05/24 6:31 UTC
Read the original article Hit count: 304

Filed under:
|
|

I am trying to write a jQuery plugin, that can be applied to a selector. Within this plugin, there are multiple calls to webservices, where callback methods are required.

The problem I am having is maintaining the current context item in the each loop of the selector.

(function($){
  $.fn.myMethod = function(){

    return this.each(function(){
      var c = $(this);
      $.api.methodToCall("", CallbackFunction);
    });

  };

  function CallbackFunction(data, result){
    // do stuff based on c
  }
})(jQuery);

Normally, you would provide the callback function straight away, which allows me to access c, however within the callback function, there potentially is another api call, so it would start to get messy. Is there any way to do this?

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery