2012-12-29 2 views
2

간단한 jquery 플러그인 내에서 함수를 호출하려고합니다. init 메소드가 너비에 30을 더하지만 add50 메소드는 오류 Uncaught TypeError: Object [object Object] has no method 'add50'을 던졌습니다. 이 오류의 원인을 확인하지 못합니다.간단한 jquery 플러그인 내에서 함수를 호출 할 수 없습니다.

jsfiddle : http://jsfiddle.net/nsshrinivasan/KnDMq/6/

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

    // Create the defaults once 
    var pluginName = 'Extender', 
     defaults = { 
      propertyName: "value" 
     }; 

    // The actual plugin constructor 
    function Plugin(element, options) { 
     this.element = element; 
     this.options = $.extend({}, defaults, options) ; 
     this._defaults = defaults; 
     this._name = pluginName; 
     this.init(this.element); 
    } 

    Plugin.prototype = { 
     init : function (element) { 
      // console.log($(this.element).width()); 
      $(element).width($(element).width() + 30); 
     }, 
     add50 : function(element){ 
      $(element).width($(element).width() + 50); 
     } 

    }; 

    $.fn[pluginName] = function (options) { 
     return this.each(function() { 
      if (!$.data(this, 'plugin_' + pluginName)) { 
       $.data(this, 'plugin_' + pluginName, 
       new Plugin(this, options)); 
      } 
     }); 
    } 

})(jQuery, window, document); 

$('#metal').Extender().add50();  

HTML :

<div id='metal'></div> 

CSS :이 같은

#metal{ 
    width: 50px; 
    height: 10px; 
    background:red; 
} 
+1

당신이 돌아 Fiddle''$ .fn.plugin'에서 this'는 선택 withing에 eleemts이다 아닌 함수 자체. 플러그인 제작과 관련하여 많은 리소스가 있습니다. 예를 들어 jQueryy 문서 메인 메뉴에서. Google에서 검색 ... 많은 자습서 – charlietfl

답변

1

를 추가해보십시오 뭔가 :

function Plugin(element, options) { 
    this.element = element; 
    this.options = $.extend({}, defaults, options) ; 
    this._defaults = defaults; 
    this._name = pluginName; 
    if (this[options]){ 
     this[options](this.element); 
    } else { 
     this.init(this.element); 
    } 
} 

그러면 모든 플러그인을 다음과 같이 만들 수 있습니다 : $('#metal').Extender('add50'); 옵션으로 메소드를 전달하십시오. 나는 당신의 바이올린을 업데이트 jsFiddle Update

+0

굉장합니다,이 작품도 - 내가 누락 된 것을 정교하게 만들 수 있습니까? 기본 플러그인을 구축 할 때 회색 영역을 이해하는 데 도움이 될 것입니다. – neelmeg

+0

이 방법으로 플러그인을 작성하지는 않습니다.하지만 통과 한 옵션이 사용 가능한 방법과 일치하는지 확인하기 위해 if/else 체크를 추가했습니다. 'this [options]'는'this.add50'와 동일합니다. 일치하는 것이 발견되면 그 메소드를 호출합니다. 그렇지 않으면 일반 init으로 대체합니다. – Syon

1

:

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

    // Create the defaults once 
    var pluginName = 'Extender', 
     defaults = { 
      propertyName: "value" 
     }; 

    // The actual plugin constructor 
    function Plugin(element, options) { 
     this.element = element; 
     this.options = $.extend({}, defaults, options) ; 
     this._defaults = defaults; 
     this._name = pluginName; 
     this.init(); 
    } 

    Plugin.prototype = { 
     init : function() { 
      // console.log($(this.element).width()); 
      $(this.element).width($(this.element).width() + 30); 
     }, 
     add50 : function(){ 
      $(this.element).width($(this.element).width() + 50); 
     } 

    }; 

    $.fn[pluginName] = function (options) { 
     return $.data(this, 'plugin_' + pluginName, new Plugin(this, options)); 
    } 

})(jQuery, window, document); 

$('#metal').Extender().add50(); 
+0

굉장한 변화가 일어난 이유를 설명해 주시겠습니까? – neelmeg

+0

'this.each ({})'대신 객체를 반환하는 중입니다. –

관련 문제