2

TL;DR: I have jQuery.myPlugin

$.fn.myPlugin = function(param, callback){
  //...which does some operations to each member of collection
  //obtained by $('.someclass').myPlugin()
  //that are represented with this variable
}

how to pass this variable - single node reference - to callback when plugin's job is done? Like this:

$.fn.myPlugin = function(param, callback){
  //...
  //...
  //when job is done:
  callback.call(this);
}
$('.someclass').myPlugin(options, function(arg){ 
  //I need arg to be this variable from plugins definition...
})

Just to mention, whatever I pass to callback.call(somevar), somevar is not available in executed anonymous callback function.

vinayakj
  • 5,591
  • 3
  • 28
  • 48
Miloš Đakonović
  • 3,751
  • 5
  • 35
  • 55

2 Answers2

3

If callback is definitely a function, you should try

callback(this);
shadowmaster13
  • 63
  • 2
  • 10
1
//when job is done:
callback(this);

call the callback directly. or use

//when job is done: 
callback.call(this, this); 

see the .call API docs.

vinayakj
  • 5,591
  • 3
  • 28
  • 48