2012-03-21 6 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

2個の構文エラーがあります。あなたが役に立つかもしれない他の

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

それはそれを行う必要があります。あなたは本当にFirebugをインストールするか、他のブラウザの開発ツールの1つを使うべきです。 Firebugのコンソールはすぐに構文エラーを指摘するエラーを投げた。 – Jason

1

気にいら:閉じ括弧がありません

return methods[method].apply(this, Array.prototype.slice.call(arguments 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()

関連する問題