什么是在方法中保存不同jquery插件实例的设置的最佳方法?

时间:2014-06-05 16:05:03

标签: jquery jquery-plugins

我有一个简单的jquery插件。我使用一些设置初始化我的插件,比如{persistent: 'new'}。 稍后,当我想更新插件的设置时,如何访问现有设置?

我不想更改其他实例的默认值。

我正在考虑将选项保存到像data('options')这样的地方,但是有更传统的方法吗?

(function() {

    var defaults = {
        persistent : 'old'
    };

    var methods = {
        init : function(options) {

            options = $.extend(defaults, {}, options);

            return this.each(function(n) {

            });

        },

        update: function(options) {

            // how to access the options in init?
            options = $.extend(defaults, {}, options);

            return this.each(function(n) {

            });
        }
    };

    $.fn.updatable = function(method) {

        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 on jQuery.updatable');
        }

    };

})(jQuery);

0 个答案:

没有答案
相关问题