2012-03-21 2 views
-1

내가 뭘 잘못 했니? jquery 문서에 제안 된대로 메서드를 분리하기 전에 작동했습니다. 콜링 : $(this).Slick('show');이 간단한 jquery 플러그인 예제에는 어떤 문제가 있습니까?

(function ($) { 

var methods = { 

    // object literals for methods 
    init: function (options) { 

     // initialize settings 
     var settings = $.extend({ 
      'location': 'center', 
      'speed': 'normal' 
     }, options); 
    }, 
    show: function() { 

     return this.each(function() { 

      // plugin code here 
      alert('success!'); 
     }); 
    }, 
    hide: function() { 

     // hide 
    } 
}; 

$.fn.Slick = function (method) { 
    // method calling logic -- jquery recommended/standard 
    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.Slick'); 
    } 
}; 
})(jQuery); 
+2

을 통해 일반적인 jQuery를 방식으로 작동합니다 jQuery 플러그인 설정을 시작하는 데 필요한 모든 것을 제공 "나는 방법을 분리하기 전에 그것은 일했다" 그것이 일했던 국가에. 그리고 다시 한 번 한 줄씩 작은 단계로 반복하십시오. 각각의 작은 반복 후에 모든 것이 예상대로 작동하는지 확인하십시오. 당신이 깨졌을 때 - 정확히 작은 부분의 변화가 무엇인지 정확히 알 수 있습니다 (** 디버깅 **이라고 함) – zerkms

답변

3

두 구문 오류가 있습니다.

누락 쉼표 :

return methods[method].apply(this, Array.prototype.slice.call(arguments 1)); 

누락 닫는 괄호 :

} else if (typeof(method === 'object' || !method) { 
+1

그렇게해야합니다. Firebug를 설치하거나 다른 브라우저의 개발자 도구 중 하나를 사용해야합니다. Firebug의 콘솔은 구문 오류를 지적하는 오류를 즉시 던졌습니다. – Jason

1

뭔가를해야만은 다른 사람이 유용하게 찾을 수 있습니다. 나는 jQuery 플러그인을 오랫동안 쓰는 데 어려움을 겪었다. 마침내 내가 사용하기 시작한 이후로 항상 나를 위해 일한이 간단한 수식을 만들었다. 코드를 롤백 ---

(function($) { 
    if (!$.myExample) { 
     $.extend({ 
      myExample: function(elm, command, args) { 
       return elm.each(function(index){ 
        // do work to each element as its passed through 
       }); 
      } 
     }); 
     $.fn.extend({ 
      myExample: function(command) { 
       return $.myExample($(this), command, Array.prototype.slice.call(arguments, 1)); 
      } 
     }); 
    } 
})(jQuery); 

는 당신에게 당신이 모두 $.myExample($("someEle"))$("someEle").myExample()

관련 문제