bumbumpaw
bumbumpaw

Reputation: 2528

Pass method parameter or option in plug-in

    (function ( $ ) {
        $.fn.greenify = function( options ) {
            var settings = $.extend({
                color: "#556b2f"
            }, options );

            return this.css({color: settings.color});
        };
              //TODO another function here
       if ( action === "open") {
           alert("Open!");
        }

        if ( action === "close" ) {
           alert("lose!");
        }

    }( jQuery ));

`$( "div" ).greenify({color: "orange"});`//setting of options

I understand the above code sets options for greenify plug in, but I also want to call the display function.

How can I achieve this kind of usage while setting of options still available for the greenify :

`$( "div" ).greenify("open");` 

and

$( "div" ).greenify(open,{color: "orange"});

Upvotes: 0

Views: 29

Answers (1)

adeneo
adeneo

Reputation: 318372

You just check if options is an object or string

$.fn.greenify = function(options) {
    if (typeof options === 'string') {
        if (options === 'open') {
            alert('open');
        } else if (options === 'close') {
            alert('close');
        }
    } else {

        var settings = $.extend({
            color: "#556b2f"
        }, options);

        return this.css({
            color: settings.color
        });
    }
}

Upvotes: 1

Related Questions