jQuery插件将使用默认选项而不是用户定义的选项

时间:2012-12-13 16:13:39

标签: javascript jquery

所以我有一个带有更新方法的插件,如果用户向div(占位符)广告新内容,则会使用该插件,一开始就好了,插件将使用插件的用户定义选项,但是一旦用户运行更新方法,它将使用默认选项而不是已定义的选项。

插件包装......

;(function($, window, document, undefined){

    //"use strict"; // jshint ;_;

    var pluginName = 'myPlugin';

    function Plugin(element, options){

        this.elm     = $(element);
        this.options = $.extend({}, $.fn[pluginName].defaults, options);

        this.init();
    };

    Plugin.prototype = {

        init: function(){

            // we run this at start...

        },

        update: function(){

            // code that we need when we need to update...

        },

    };

    $.fn[pluginName] = function(option) {
        var options = typeof option == "object" && option;

        return this.each(function() {
            var $this = $(this);
            var data  = new Plugin($this, options);

            if(!$.data($this, pluginName)){         
                $.data($this, pluginName, data);
            }

            if(typeof option == 'string'){
                data[option]();
            }

        });
    };

    $.fn[pluginName].defaults = {
        name: 'Hank',
        ...
        ...
    };


})(jQuery, window, document);

// inline js

    $('#container').myPlugin({name: 'NOT Hank...'});

    $('#updatelink').click(function(e){
       $('#container').myPlugin('update');
    }):

0 个答案:

没有答案