tomoguisuru
tomoguisuru

Reputation: 402

jQuery Plugin - passing a function / method as an option

Noob question here, my basic setup looks like this:

(function($) {
    $.fn.imageSlider = function(options) {
        var defaults = {
            columns: 3,
            imgHeight: 50,
            imgWidth: 75,
            jsonScript: '',
            jsonData: {},
            onComplete: $.noop  // I want a user supplied method here
        };

        var options = $.extend(defaults, options);

        // Exec plugin here

        if($.isFunction(options.onComplete)) {
            // call user provided method
            options.onComplete.call();
        }
})(jQuery)

plugin call

 $('#gallery').imageSlider({
    columns: 6,
    jsonScript: './scripts/galley.php?id=1'
 });

The plugin works fin but will never call the user supplied onComplete function

What do I need to do to get the onComplete method to work?

Upvotes: 1

Views: 3793

Answers (1)

scurker
scurker

Reputation: 4753

One issue that I can immediately see is that you're not properly closing your function definition.

(function($) {
  $.fn.imageSlider = function(options) {

    // ...setup options and code...

  }; // <-- missing this
})(jQuery);

Other than that, everything looks to be working fine: http://jsfiddle.net/y4rkS/

Upvotes: 1

Related Questions