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.

2
  • 1
    //when job is done: callback(this); call the callback directly. or //when job is done: callback.call(this, this); see the .call docs. Commented Oct 29, 2015 at 22:23
  • @vinayakj write and answer, that is correct! Commented Oct 29, 2015 at 22:28

2 Answers 2

3

If callback is definitely a function, you should try

callback(this);
Sign up to request clarification or add additional context in comments.

4 Comments

That's the first thing I've tried: no results, can't catch anything in nonymous function.
This is correct answer. @Miloshio It will work as expected. If it doesn't work for you then show how you use callback function.
Is the function being called at all? Just not getting 'this' passed?
Function is being called and, what is this in callback.call(this) doesn't show in anonymous function passed as callback.
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.

1 Comment

that's it. Waiting for time limit to accept your answer. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.