0

在回答这个问题(MVC 5 - 无法运行全球化)时,我用一堆“<sript src="..." 声明和一些 js 解决了这个问题。

我没有管理的内容:我想捆绑脚本。如果我这样尝试(摘自 bundleConfig.cs):

 bundles.Add(new ScriptBundle("~/bundles/jqueryvalDe").Include(
                    "~/Scripts/jquery.validate.js",
                    "~/Scripts/jquery.validate.unobtrusive.js",
                    "~/Scripts/cldr.js",
                        "~/Scripts/cldr/event.js",
                        "~/Scripts/cldr/supplemental.js",
                        "~/Scripts/cldr/unresolved.js",
                        "~/Scripts/globalize.js",
                        "~/Scripts/globalize/currency.js",
                        "~/Scripts/globalize/number.js",
                        "~/Scripts/globalize/date.js",
                        "~/Scripts/globalize/plural.js",
                        "~/Scripts/globalize/relative-time.js",
                        "~/Scripts/globalize/unit.js",
                        "~/Scripts/jquery.validate.globalize.js"
                    ));         

但我从 JavaScript 中得到一个错误:“Globalize”未定义

看来,脚本的顺序改变了......

目前我使用一种解决方法:我编写了一个自定义 HTM-Helper。

 public static class GermanDateValidationExtension
{
    public static MvcHtmlString ScriptsForGermanDateValidation(this HtmlHelper helper)
    {
        return new MvcHtmlString("<script src=\"~/ Scripts / jquery.validate.js\"></script>\n" +
                                 "<script src = \"~/Scripts/jquery.validate.unobtrusive.js\"></script>\n" +
                                 "<script src = \"~/Scripts/cldr.js\"></script>\n" +
                                 "<script src = \"~/Scripts/cldr/event.js\"></ script>\n" +
                                 "<script src = \"~/Scripts/cldr/supplemental.js\"></script>\n" +
                                 "<script src = \"~/Scripts/cldr/unresolved.js\"></script>\n" +
                                 "<script src = \"~/Scripts/globalize.js\"></script>\n" +
                                 "<script src = \"~/Scripts/globalize/currency.js\" ></script>\n" +
                                 "<script src = \"~/Scripts/globalize/number.js\"></script>\n" +
                                 "<script src = \"~/Scripts/globalize/date.js\"></script>\n" +
                                 "<script src = \"~/Scripts/globalize/plural.js\" ></script>\n" +
                                 "<script src = \"~/Scripts/globalize/relative-time.js\"></script>\n" +
                                 "<script src = \"~/Scripts/globalize/unit.js\"></script>\n" +
                                 "<script src = \"~/Scripts/jquery.validate.globalize.js\"></script>\n" +
                                 "<script>\n" +
                                 "$.when(\n" +
                                 "$.get(\"/Scripts/cldr/main/de/ca-gregorian.json\"),\n" +
                                 "$.get(\"/Scripts/cldr/main/de/numbers.json\"),\n" +
                                 "$.get(\"/Scripts/cldr/supplemental/likelySubtags.json\"),\n" +
                                 "$.get(\"/Scripts/cldr/supplemental/timeData.json\"),\n" +
                                 "$.get(\"/Scripts/cldr/supplemental/weekData.json\")\n" +
                                 ").then(function() {\n" +
                                 "return [].slice.apply(arguments, [0]).map(function(result) {\n" +
                                 "return result[0];\n" +
                                 "});\n" +
                                 "}).then(Globalize.load)\n" +
                                 ".then(function() {\n" +
                                 "Globalize.locale(\"de-DE\");\n" +
                                 "});\n" +
                                 "</ script > ");
    }

}

并在视图中使用它:

...
@section Scripts {      
@Html.ScriptsForGermanDateValidation()
}  
...

我的问题是:如何正确捆绑脚本?

4

2 回答 2

0

解决方案是 - Tetsuya Yamamoto 所说的:编写你自己的 orderer。

public IEnumerable<BundleFile> OrderFiles(BundleContext context, IEnumerable<BundleFile> files)
    {
        return files;
    }

并使用它:

 var qvDeBundle  =new ScriptBundle("~/bundles/jqueryvalDe").Include(
                    "~/Scripts/jquery.validate.js",
                    "~/Scripts/jquery.validate.unobtrusive.js",
                    "~/Scripts/cldr.js",
                        "~/Scripts/cldr/event.js",
                        "~/Scripts/cldr/supplemental.js",
                        "~/Scripts/cldr/unresolved.js",
                        "~/Scripts/globalize.js",
                        "~/Scripts/globalize/currency.js",
                        "~/Scripts/globalize/number.js",
                        "~/Scripts/globalize/date.js",
                        "~/Scripts/globalize/plural.js",
                        "~/Scripts/globalize/relative-time.js",
                        "~/Scripts/globalize/unit.js",
                        "~/Scripts/jquery.validate.globalize.js"
                    );         
        qvDeBundle.Orderer=new UnorderBundleOrderer();
        bundles.Add(qvDeBundle);
于 2017-07-05T09:11:03.460 回答
0

问题似乎源于jquery.validate.globalize.js之前渲染的捆绑订单globalize.js,它使用为知名库而建立的“依赖树”,并且它们的自定义扩展将在其他人之前渲染。因此,为了根据需要设置加载顺序,请创建一个实现IBundleOrder接口及其相应扩展方法的类:

// from Sebastián Rojas (/a/26602075)
class UnorderedBundleOrderer : IBundleOrderer
{
    public IEnumerable<BundleFile> OrderFiles(BundleContext context, IEnumerable<BundleFile> files)
    {
        return files;
    }
}

static class BundleExtensions
{
    public static Bundle UnorderedBundling(this Bundle bundle)
    {
        bundle.Orderer = new UnorderedBundleOrderer();
        return bundle;
    }
}

然后您可以在 BundleConfig.cs 文件中使用上面的自定义捆绑顺序:

public static void RegisterBundles(BundleCollection bundles)
{
    bundles.Add(new ScriptBundle("~/bundles/jqueryvalDe")
           .UnorderedBundling()
           .Include(
            "~/Scripts/jquery.validate.js",
            "~/Scripts/jquery.validate.unobtrusive.js",
            ...
            ...
            "~/Scripts/globalize.js",
            ...
            ...
            "~/Scripts/jquery.validate.globalize.js"
    ));
}

相关问题:

ASP.NET MVC - 捆绑配置顺序

捆绑中文件的排序 - 已知的库是什么?

于 2017-07-05T09:11:26.520 回答