开发者

jQuery Plugin - passing a function / method as an option

开发者 https://www.devze.com 2023-03-16 06:59 出处:网络
Noob question here, my basic setup looks like this: (function($) { $.fn.imageSlider = function(options) {

Noob question here, my basic setup looks like this:

(function($) {
    $.fn.imageSlider = function(options) {
        var defaults = {
            columns: 3,
            im开发者_开发技巧gHeight: 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?


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消