0

I created a simple Jquery function that takes a few parameters and comes up with some kind of result. I need to be able to pass this result to the call Back function of the jquery plugin.

(function( $ ){
   $.fn.newPlugin= function(params,CBfunction){

    //do some stuff;
    var result = "something";
    CBfunction.call(this);

}; 
})( jQuery );

when I use the plugin I want to be able to parse the result of the plugin to the callback function:

$(elm).newPlugin(params, function(params, this.result){
  //this.resullt comes from the plugin ('something');

})

Is this possible?

For example look at the variable "myVar":

(function( $ ){
   $.fn.newPlugin= function(a,b,CBfunction){

    myVar = a+b;

    CBfunction.call(this);

}; 
})( jQuery );

I use the plugin:

$(elm).newPlugin(a,b,function(){
  alert("the result of "+a+" + "+b+" is:"+myVar);
})

myVar comes from the plugin and used in the callback function, is there any way to do this?

Thanks.

5
  • I'm not sure what you mean by "parse the result of the plugin to the callback"... Commented Feb 9, 2014 at 8:23
  • Is it your own plugin that you are writing? Commented Feb 9, 2014 at 8:24
  • I am guessing he actually means "pass" not "parse". Commented Feb 9, 2014 at 8:25
  • lets say that $(elm).height() returns the height of an element, I want to use this value into the callback functon, height doesn't have one, but fadeIn does, "imagine: that fadeIn() returns some value and you want to use that value n the callback function $(elm).fadeIn(100, function(value){ //do something with value }); Commented Feb 9, 2014 at 8:25
  • I modified my question, maybe it is more clear now, thanks. Commented Feb 9, 2014 at 8:46

1 Answer 1

2

Why not just pass the result when you call the callback? Like this:

(function( $ ){
   $.fn.newPlugin= function(params,CBfunction){

    //do some stuff;
    var result = "something";
    CBfunction.call(this, result);
};
})(jQuery);

Then you need to modify your call to look like this (by the way, notice that I removed the 'param' parameter form the callback, because only one parameter is being passed):

$(elm).newPlugin(params, function(result){
  // result comes from the plugin ('something');
})
Sign up to request clarification or add additional context in comments.

12 Comments

will this allow me to pass more variables and "result" ?
Yes, you can pass as many variables as you want, just like any other function call. The only difference ".call" has over a normal function call is that it allows you to specify what the "this" keyword value should be inside the callback.
this means that the person to use the plugin must use "result" as a specific variable coming from the plugin, how does the function will now who is result if I pass 2 or 3 or n variables?
I just realised you also had a mistake in your callback definition in that you have another parameter call 'param', which shouldn't be there.
To answer your question, this is something that you have to tell your users about, e.g. in the documentation. Just like any callback, the user need to know what parameters is it going to receive.
|

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.