在命名空间jQuery插件中跨方法共享设置

在命名空间jQuery插件中跨方法共享设置,jquery,plugins,Jquery,Plugins,我正在编写一个插件,并遵循jQuery文档中关于名称空间和多种方法的推荐做法 My init()负责使用$.extend()合并默认设置和自定义设置,但是我不知道如何使这些选项在init()方法之外可用。说出那个调用并使用初始化我的插件 $("a").myplugin({debug:false}); 以后调用时如何引用调试属性 $("a").myplugin("someMethod")? 一个粗略的例子是: (function( $ ){ var methods

我正在编写一个插件,并遵循jQuery文档中关于名称空间和多种方法的推荐做法

My init()负责使用$.extend()合并默认设置和自定义设置,但是我不知道如何使这些选项在init()方法之外可用。说出那个调用并使用初始化我的插件

$("a").myplugin({debug:false}); 
以后调用时如何引用调试属性

$("a").myplugin("someMethod")?  
一个粗略的例子是:

   (function( $ ){
        var methods = {
            init: function(customSettings) {
                var options = {
                    debug: true
                }
                return this.each(function () {
                   if (customSettings) {
                       $.extend(options, customSettings);
                   }
                });
            },
            someMethod: function() {
               if(options.debug) { // <-- How can I access options here?
                   // do something
               }
            }
         }
    })( jQuery );

    $.fn.myplugin = 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.tbwaga');
                }
            };
(函数($){
var方法={
初始化:函数(自定义设置){
变量选项={
调试:正确
}
返回此。每个(函数(){
如果(自定义设置){
$.extend(选项、自定义设置);
}
});
},
someMethod:function(){

如果(options.debug){/我没有查看你的插件模板,但我想分享一下……它在jQuery存储的数据中添加了对DOM对象的反向引用。这使得从插件闭包外部访问插件函数和/或变量非常容易

下面是一个更详细地描述插件结构的示例

因此,要访问插件中的函数,只需使用数据对象:

$('a').data('myplugin').function_name();
甚至从插件设置中获取变量

var height = $('a').data('myplugin').options.height;

但是为了回答您的问题,为了使您的选项可用于闭包内的其他函数,只需在init函数外定义option变量:

(function( $ ){
        var options, methods = {
            init: function(customSettings) {
                options = {
                    debug: true
                }

如您所说,您可以考虑将默认设置设置在init方法之外。 我尝试遵循同样的教程,并提出了以下结合设置和方法的代码,但我遇到了其他一些缺点

  (function( $ ){
     var config, methods = {
        init: function(customSettings) {
            var config = {
                debug: true
            }
            return this.each(function () {
               if (customSettings) {
                   $.extend(config, customSettings);
               }
            });
        },
        someMethod: function(arg) {
           if(options.debug) {
               // do something
               alert('debug ' + arg);
           } else {
               alert('no debug' + arg);
           }
        }
    }

    $.fn.myplugin = 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.myplugin' );
        }
    };

})( jQuery );
但当你这样称呼它时:

$('p.one').myplugin({ 'debug' : 'false' });
对于第二段,debug仍然是错误的

$('p.two').myplugin('someMethod', 'hmm!');
我首先需要用“true”再次初始化段落,以便能够调试它

$('p.two').myplugin({ 'debug' : 'true' });
$('p.two').myplugin('someMethod', 'hmm!');

我在教程中看到了什么吗?

不要扩展选项本身,扩展到一个空对象:
$。扩展({},选项,自定义设置);
此外,您需要存储选项(反向引用我链接的帖子中提到的DOM)所以你的方法可以访问它们。@fudgey多亏了你的帮助,我想我已经搞定了!我已经用你的评论编辑并重播了上面的代码示例。