是否可以在 Asp.Net MVC 中围绕 ScriptBundle 包装单个 jQuery 文档就绪事件?

Is it possible to wrap a single jQuery document ready event around a ScriptBundle in Asp.Net MVC?

为了避免在 Asp.Net MVC ScriptBundle 中为每个文件多次调用 jQuery .ready() 事件,是否可以将 ready 事件包装在整个包中?

以下每个文件都包含围绕 JavaScript 代码的 $( document ).ready(function() { } 代码...

bundles.Add(new ScriptBundle("~/bundles/jquery").Include(
                        "~/Scripts/jquery-{version}.js",
                        "~/Scripts/core.js",
                        "~/Scripts/application.js",
                        "~/Scripts/controls.js",
                        "~/Scripts/custom.js"));

我可以创建两个 js 文件,一个包含 $( document ).ready(function() {,另一个只包含一个右括号。然后将这些文件添加到现有捆绑包的开始和结束,但是有更好的方法吗?

A similar question 已被要求使用 CoffeScript 文件串联,但我无法轻松地将解决方案转换为 Asp.Net 捆绑。

如果您查看的是您编写的 JavaScript 文件(而不是 jquery-{version}.js),则创建一个可从您在页面上调用的文件外部公开访问的方法。

var Application = (function () {
    // All your code

    function init() {
        // All your code that you have inside $(document).ready()
    }

    return {
        init: init
    };
}());

对所有文件执行上述操作,然后在任何需要它的页面上执行上述操作,或者在您的 _Layout 页面中执行 运行 在每个页面的底部都有以下内容

<script type="text/javascript">
    $(document).ready(function () {
        Application.init();
        // etc for the other files.
    });
</script>