2011-11-28 73 views
0

,當我在一個這樣的方式綁定自定義插件它的工作原理確定:自定義jQuery插件:「插件」不是一個函數

<script type="text/javascript"> 
    $("#MyGrid").customFilter({ postUrl: '@(Url.Action("SearchOffers", "Department"))' }); 
</script> 

,但如果我想等到文檔準備就緒:

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#MyGrid").customFilter({ postUrl: '@(Url.Action("SearchOffers", "Department"))' }); 
    }); 
</script> 

$(「#MyGrid」)。customFilter不是函數錯誤出現。爲什麼?

自定義插件:

(function ($) { 

    var theGrid; 
    var filterTimeout; 
    var mouseIsInside = false; 
    var postUrl; 

    var methods = { 
     init: function (options) { 

      return this.each(function() { 
       postUrl = options.postUrl; 
       theGrid = $(this); 
       ... 
      }); 
     }, 
    method2: function() { 
     // ... 
    } 
}; 

    $.fn.customFilter = function (method) { 

     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.customFilter '); 
     } 

    }; 

    //private functions 
    function isFilterable(th) { 
     var name = th.children("a").text(); 
     return (name.length > 0) ? true : false; 
    } 
... 
})(jQuery); 
+0

您可以發佈您customFilter,或至少在第幾行? – Martin

+0

您的'方法'對象未正確關閉。希望複製粘貼錯誤? – Blazemonger

+0

更新後,就像現在一樣。 – 1gn1ter

回答

0

該腳本文件應在Telerik的()ScriptRegistrar()註冊

@Html.Telerik().ScriptRegistrar().DefaultGroup(group => group.Add("~/Scripts/gridFilter.js"))