yckart
yckart

Reputation: 33408

jQuery plugin return value from method and still preserve chainability

I currently reworking my personal jQuery plugin starting point, but I stuck at a small and annoying problem. If a public method is called, it returns the the object it was called from and not the value.

Here's my actual point where I start from:

;(function($, window, document, undefined) {
    'use strict';

    var pluginName = 'defaultPluginName',
        defaults = {
            foo: 'foo',
            bar: 'bar'
        },
        settingsKey = pluginName + '-settings';


    var init = function(options) {

        var elem = this,
            $elem = $(this),
            settings = $.extend({}, defaults, options);

        $elem.data(settingsKey, settings);

    };


    var callMethod = function(method, options) {
        var methodFn = $[pluginName].addMethod[method],
            args = Array.prototype.slice.call(arguments);
        if (methodFn) {
            this.each(function() {
                var opts = args.slice(1),
                    settings = $(this).data(settingsKey);
                opts.unshift(settings);
                methodFn.apply(this, opts);
            });
        }
    };

    $[pluginName] = {
        settingsKey: settingsKey,
        addMethod: {
            option: function(settings, key, val) {
                if (val) {
                    settings[key] = val;
                } else if (key) {
                    return settings[key]; // returns the value from settings
                }
            }
        }
    };

    $.fn[pluginName] = function(options) {
        if (typeof options === 'string') {
            callMethod.apply(this, arguments);
        } else {
            init.call(this, options);
        }
        return this;
    };

}(window.jQuery || window.Zepto, window, document));

However, I initialize the plugin it and call a method...

$('div').defaultPluginName();
console.log($('div').defaultPluginName('option', 'foo'));

it returns: [<div>, <div>, prevObject: jQuery.fn.jQuery.init[1], context: #document, selector: "div"] and not 'foo' as (from where the comment is) excepted.

So the question is, is it possible to return the value from a public method and still preserve the chainability? And if you have time and fun to help me, i would be glad about some examples ;)

Upvotes: 0

Views: 979

Answers (1)

yckart
yckart

Reputation: 33408

I've solved it out myself:

$.fn[pluginName] = function(options) {
    var returns;
    if (typeof options === 'string') {
        var args = arguments,
            methodFn = $[pluginName].addMethod[args[0]];

        if (methodFn) {
            this.each(function() {
                var opts = Array.prototype.slice.call(args, 1),
                    settings = $.data(this, settingsKey);
                opts.unshift(settings);
                returns = methodFn.apply(this, opts);
            });
        }
    } else {
        new Plugin(this, options).init();
    }
   return returns !== undefined ? returns : this;
};

Upvotes: 1

Related Questions