In jQuery plugins, which way do you think it’s best to allow a function to be hooked in your plugin – trough triggers, or options (arguments) passed in the plugin function?
$.trigger('myplugin_completed', someData);
$(document).bind('myplugin_completed', function(event, someData){ ... });
vs
myPluginOptions.onComplete(someData);
$('.stuff').myPlugin({onComplete: function(someData){ ... }});
Solution
I think the best solution – at least in the case you described (maybe not in all the possible cases) – is to combine both in the following way:
and this line:
is responsible for binding the event handler. It can be called also when someone passes
onCompletecallback within options when initializing your plugin (of course selector should be adjusted to meet the one used by the code initializing your pugin).Summary
To sum up, you could:
completed),myplugin),.on()function (available and preferred since jQuery 1.7),onCompleteoption is passed to your plugin, there is no problem in binding it within the code initializing the plugin (so from within the plugin, using the.on()function, binding event handler to your event name within your even namespace).