2013-07-13 6 views
0

플러그인에서 메소드 내부의 하위 메소드에 액세스하려면 어떻게해야합니까? 예를 들어, init 방법, 나는jquery 플러그인에서 메소드 내부의 하위 메소드에 액세스하는 방법은 무엇입니까?

$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); 

이 가능하거나 일을 다른 더 좋은 방법인가, manage 내부의 page 방법에 액세스하려면? 자식 메서드를 부모 메서드 안에 넣고 싶습니다.

답변

0

코드로 manage.page에 액세스 할 수 없습니다. 추가 속성이 아닌 methods의 속성 만 호출 할 수 있습니다.

manage 개체에 page()을 넣고 "페이지"를 플러그인 기능에 전달할 수 있습니다.

+0

감사합니다. 'manage' 객체에'page()'를 어떻게 두어야합니까? 몇 가지 예를 보여 주시겠습니까 ...? – laukok

+1

그는 사용할 수 없습니다 :'methods.manage.page.apply (this, [o]); ' –

+0

@lauthiamkok 기본 JavaScript 질문입니다. 자바 스크립트에서 객체가 작동하는 방식을 연구하는 것이 좋습니다. 'manage' 속성을 새로운 인터페이스를 반환하는 함수로 만들 수 있습니다. 그런 다음 메서드를 호출합니다 (그렇게하면 * jQuery 체인 외부에있을 것입니다). – alex

0

내가 바로 여기 여부를 그 일을하지만, 오전 확실하지 내 솔루션입니다 ...

(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); 

그리고 그것은 긴/지루하지만, 작품을 보인다

// 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); 

page 또 다른 플러그인, .

관련 문제