【问题标题】:How to access the sub method inside a method from a jquery plugin?如何从 jquery 插件访问方法内的子方法?
【发布时间】:2013-07-11 19:02:55
【问题描述】:

如何从插件访问方法中的子方法?比如在init方法中,我想访问manage里面的page方法,

$this.cms(manage.page); // ReferenceError: manage is not defined

基本结构,

(function($){

    var methods = {

        defaults: function(options) {

            // Default options.
            var defaults = {
                setup: {
                    directory: ""
                }           
            }

            // Always leave this line here.
            var options = $.extend(defaults, options);

            // Return the processed options.
            return options;

        },

        init : function( options ) {

            // Must declare a this as the plugin's root itself.
            var $this = this;

            // Call the local method from the plugin itself to get the processed options.
            var o = $this.cms('defaults',options);
            //alert("nothing to load");


            $this.cms(manage.page);
        },

        manage : {

            page: function(options){

                // Must declare a this as the plugin's root itself.
                var $this = this;

                // Call the local method from the plugin itself to get the processed options.
                var o = $this.cms('defaults',options);

                alert("page method");

            },
            menu: function(options){
            }

        },

        add : function( options ) {

        },

        erase : function( options ) { 

        },

        remove: function(object,options) { // to be developed.

        }
    };

    $.fn.cms = function( method ) {

        // @reference: http://docs.jquery.com/Plugins/Authoring#Namespacing
        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 );  // always change 'init' to something else if you different method name.
        } else {
            $.error( 'Method ' +  method + ' does not exist on jQuery.cms' );
        }

        return this; // this is needed to ensure chainability @reference: http://stackoverflow.com/questions/14008121/jquery-namespace-how-to-pass-default-options-from-one-method-to-the-subsequence

    };

})(jQuery);

是否有可能或任何其他更好的方法?我只想在父方法中有子方法。

【问题讨论】:

    标签: jquery jquery-plugins methods jquery-1.9


    【解决方案1】:

    您无法使用您拥有的代码访问manage.page。它只允许您调用methods 的属性,而不是其他属性。

    您可以将page() 放在manage 对象上并将“页面”传递给插件的函数。

    【讨论】:

    • 谢谢。如何将page() 放在manage 对象上?你能给我举几个例子吗...?
    • @lauthiamkok 这是一个基本的 JavaScript 问题。我建议研究对象如何在 JavaScript 中工作。您可以将manage 属性设为一个返回该新接口的函数,然后调用其中的方法(如果您这样做,您将位于 jQuery 链 之外)。
    • @roasted 当然可以,但是你需要设计一个接口来通过字符串调用它(目前,它只支持methods 上的直接成员)。
    【解决方案2】:

    不确定我做对与否,但这是我的解决方案...

    (function($){
    
        var methods = {
    
            defaults: function(options) {
    
                // Default options.
                var defaults = {
                    setup: {
                        directory: ""
                    }           
                }
    
                // Always leave this line here.
                var options = $.extend(defaults, options);
    
                // Return the processed options.
                return options;
    
            },
    
            init : function( options ) {
    
                // Must declare a this as the plugin's root itself.
                var $this = this;
    
                // Call the local method from the plugin itself to get the processed options.
                var o = $this.cms('defaults',options);
                //alert("nothing to load");
    
    
                $this.cms("manage").page();
            },
    
           manage : function(options){
    
                // Must declare a this as the plugin's root itself.
                var $this = this;
    
                // Call the local method from the plugin itself to get the processed options.
                var o = $this.cms('defaults',options);
    
                // Set the list for holding properties.
                var properties = {
                    page : function(){ $.fn.page(); }
                }
    
                // Return the property.
                return properties;
    
            },
    
            add : function( options ) {
    
            },
    
            erase : function( options ) { 
    
            },
    
            remove: function(object,options) { // to be developed.
    
            }
        };
    
        $.fn.cms = function( method ) {
    
            // @reference: http://docs.jquery.com/Plugins/Authoring#Namespacing
            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 );  // always change 'init' to something else if you different method name.
            } else {
                $.error( 'Method ' +  method + ' does not exist on jQuery.cms' );
            }
    
            return this; // this is needed to ensure chainability @reference: http://stackoverflow.com/questions/14008121/jquery-namespace-how-to-pass-default-options-from-one-method-to-the-subsequence
    
        };
    
    })(jQuery);
    

    然后是page的另一个插件,

    // A namepace structure:
    (function($){
    
        var methods = {
    
            defaults: function(options) {
    
                // Default options.
                var defaults = {
                    setup: {
                        directory: ""
                    }           
                }
    
                // Always leave this line here.
                var options = $.extend(defaults, options);
    
                // Return the processed options.
                return options;
    
            },
    
            init : function( options ) {
    
                // Must declare a this as the plugin's root itself.
                var $this = this;
    
                // Call the local method from the plugin itself to get the processed options.
                var o = $this.page('defaults',options);
    
                alert("a page plugin: list page");
            },
    
            remove: function(object,options) { // to be developed.
    
            }
        };
    
        $.fn.page = function( method ) {
    
            // @reference: http://docs.jquery.com/Plugins/Authoring#Namespacing
            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 );  // always change 'init' to something else if you different method name.
            } else {
                $.error( 'Method ' +  method + ' does not exist on jQuery.page' );
            }
    
            return this; // this is needed to ensure chainability @reference: http://stackoverflow.com/questions/14008121/jquery-namespace-how-to-pass-default-options-from-one-method-to-the-subsequence
    
        };
    
    })(jQuery);
    

    这看起来很长/很乏味,但很有效。

    【讨论】:

      猜你喜欢
      • 2012-04-25
      • 1970-01-01
      • 2021-07-09
      • 1970-01-01
      • 2019-06-07
      • 1970-01-01
      • 1970-01-01
      • 2012-12-21
      • 1970-01-01
      相关资源
      最近更新 更多