1

I'm writing a plugin for an accordion, that auto rotates. I am trying to add a callback that will fire each time a new slide is presented and if the user has set it in the options on init. So, for example:

Callback set in the options:

var defaults = 
    {
    callback: function(arg) {}     
    };
var options = $.extend(defaults, options);

Rotation script:

function autorotation() {
   var arg = 'hi'; 
   options.callback.call(this);
}

Init plugin script:

$('element').myplugin({ 
    callback: function(arg) {
        alert(arg);
    }
});

My question is, how do I write this correctly so that I can successfully pass the argument each time the slides rotate to the client outside of the plugin? I hope this make sense to everyone. I tried to be as simple as possible.

Bo Persson
  • 86,087
  • 31
  • 138
  • 198
Jim
  • 31
  • 2
  • 1
    Well @Jim, this is a bit selfish. Please, redo your question. Your question could help someone else, no matter how stupid it was. At least in respect to the person that take his free-time to answer you. – Roko C. Buljan Jun 22 '11 at 23:16
  • Question restored to save the answer. – Bo Persson Jun 23 '11 at 14:34

1 Answers1

2

Just add the "arg" variable and any other variables you want as parameters of the "call" method call.

var arg = "hello";
var anotherarg = "world";
options.callback.call(this, arg, anotherarg);

And in the callback they'll be available as arguments.

$('element').myplugin({ 
    callback: function(thefirstarg, thesecondarg) {
        alert(thefirstarg);//alerts "hello"
        alert(thesecondarg);//alerts "world"
    }
});
Abishai Gray
  • 525
  • 2
  • 7
  • 1
    Just for completeness: [discussion of apply and call functions](http://stackoverflow.com/questions/1986896/what-is-the-difference-between-call-and-apply) – Manuel Leuenberger Jun 22 '11 at 23:06