自定义jquery插件:'plugin'不是一个函数

时间:2011-11-28 14:07:39

标签: asp.net-mvc jquery-plugins telerik-mvc

当我以这种方式绑定自定义插件时,它可以正常工作:

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

1 个答案:

答案 0 :(得分:0)

脚本文件应该在Telerik()中注册.ScriptRegistrar()

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