user1032531
user1032531

Reputation: 26341

Extending jQuery plugin and changing default values

I have a plugin called "myPlugin" which uses the commonly used design pattern of default values which can be overridden upon initialization.

Instead of passing the overriding values upon initialization, I would like to extend the plugin as "myPlugin2" and change the default values so that when I initiate the extended plugin, it already has the desired new default values.

I've played around with adding new methods to the extended plugin, but I can't figure out how to change the default values.

In other words, I want the two lines of code to provide identical results.

$("body").myPlugin({'prop1':'prop1 modified','default_func4':function () {console.log('default_func4 modified')}});
$("body").myPlugin2();

How can I extend a jQuery plugin and change the default values?

http://jsfiddle.net/L1ng37wL/2/

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title>Testing</title>  
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js" type="text/javascript"></script>
        <style type="text/css">
        </style> 
        <script type="text/javascript"> 
            (function ($) {

                var defaults = {
                    'prop1'  : 'prop1',
                    'default_func4'  : function () {console.log('default_func4');},
                    'default_func5'  : function () {console.log('default_func5');}
                };
                var methods = {
                    init: function (options) {
                        console.log("init");
                        console.log('defaults',defaults);
                        console.log('options',options);
                        var settings = $.extend({}, defaults, options);
                        console.log('settings',settings);
                        console.log('The value of "prop1" is '+settings.prop1);
                        settings.default_func4.call()
                    },
                    func1: function () {console.log("func1");},
                    func2: function () {console.log("func2");}
                };

                $.fn.myPlugin = function (method) {
                    // Method calling logic
                    if (methods[method]) {
                        return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
                    } else if (typeof method === 'object' || !method) {
                        return methods.init.apply(this, arguments);
                    } else {
                        $.error('Method ' + method + ' does not exist');
                    }
                };

            })(jQuery);

            (function ($) {

                var methods = {
                    'func1': function () {console.log("myPlugin2: func1");},
                    'func3': function () {console.log("myPlugin2: func3");}
                }
                $.fn.myPlugin2 = function (method) {
                    //HOW DO I CHANGE defaults.prop1 and defaults.default_func5?????
                    // Method calling logic
                    if (methods[method]) {
                        return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
                    } else if ((typeof method === 'object' || !method) && methods.init) {
                        return methods.init.apply(this, arguments);
                    } else {
                        try {
                            return $.fn.myPlugin.apply(this, arguments);
                        } catch (e) {
                            $.error(e);
                        }
                    }
                }

            })(jQuery);

            $(function(){
                $("body").myPlugin({
                    'prop1':'prop1 modified',
                    'default_func4':function () {console.log('default_func4 modified')}
                });

                $("body").myPlugin2();
                //$("body").myPlugin2('func1');
                //$("body").myPlugin2('func2');
                //$("body").myPlugin2('func3');
            });

        </script>
    </head>

    <body>
    </body> 
</html> 

Upvotes: 0

Views: 529

Answers (1)

Remco van Dalen
Remco van Dalen

Reputation: 304

The double check on the method parameter feels a bit weird to me, but if I replace your single line in the try-block to the code below, the thing works like it should, while still allowing you to supply an object with even different options.

var args = arguments;
if (typeof method === 'object' || !method) {
   // Fill args with the new defaults.
   args = {
      'prop1': 'prop1 modified',
      'default_func4': function () {
         console.log('default_func4 modified')
      }
   };
   $.extend(args, method);
   args = [args];
}
return $.fn.myPlugin.apply(this, args);

Upvotes: 1

Related Questions