【问题标题】:How to use Kendo UI MVC Extensions with require js?如何将 Kendo UI MVC Extensions 与 require js 一起使用?
【发布时间】:2013-08-29 07:33:49
【问题描述】:

我有一个如下所示的控制器:

using System.Collections.Generic;
using System.Web.Mvc;

using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;

namespace KendoMvcApplication.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetData([DataSourceRequest] DataSourceRequest req)
        {
            var products = CreateProducts();
            var result = products.ToDataSourceResult(req);
            return Json(result);
        }     

        private static IEnumerable<Product> CreateProducts()
        {
            for (int i = 1; i <= 20; i++)
            {
                yield return new Product
                {
                    ProductId = i,
                    ProductName = "Product " + i,
                    ProductPrice = i * 2.5
                };
            }
        }
    }

    public class Product
    {
        public int ProductId { get; set; }
        public string ProductName { get; set; }
        public double ProductPrice { get; set; }
    }
}

还有一个看起来像这样的视图:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <link href="~/Content/kendo.common.min.css" rel="stylesheet" />
    <link href="~/Content/kendo.default.min.css" rel="stylesheet" />
    <script type="text/javascript" src="~/Scripts/require.js"></script>
</head>
<body>            
<div id="grid"></div>
<script type="text/javascript">            
    require.config({
        baseUrl : '@Url.Content("~/Scripts")',
        paths: {
            'jquery': 'jquery-2.0.3.min',
            'kendo': 'kendo-ui'
        },
        shim: {
            'jquery': {
                exports: 'jQuery'
            }
        }            
    });           
    require(['jquery', 'kendo/kendo.grid.min'], function ($) {
        $(document).ready(function () {
            $('#grid').kendoGrid({
                dataSource: {
                    schema: {
                        data: 'Data',
                        total: 'Total',
                        aggregates: 'AggregateResults',
                        model: {
                            id: "ProductId",
                            fields: {
                                ProductName: { type: "string" },
                                ProductPrice: { type: "number" }
                            }
                        }
                    },
                    transport: {
                        read: {
                            url: "@Url.Action("GetData", "Home")",
                            dataType: "json",
                            method: "post"
                        }
                    },
                    pageSize: 10,
                    serverPaging: true,
                    serverSorting: true,
                    type: "aspnetmvc-ajax"
                },
                sortable: {
                    mode: "single"
                },
                columns: ["ProductName", "ProductPrice"],
                scrollable: false,
                pageable: true
            });
        });            
    });
</script>
</body>
</html>

而我的目录结构是:

  • Scripts/kendo-ui/*(所有 kendo 文件,包括 mvc 文件)
  • 脚本/require.js
  • 脚本/jquery-2.0.3.min.js

几乎可以工作,只是没有应用服务器端排序。

这是因为从未下载过 kendo.aspnet.mvc.min.js 文件(当然,因为 require JS 对此一无所知),因此我尝试了以下方法进行补救:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <link href="~/Content/kendo.common.min.css" rel="stylesheet" />
    <link href="~/Content/kendo.default.min.css" rel="stylesheet" />
    <script type="text/javascript" src="~/Scripts/require.js"></script>
</head>
<body>            
<div id="grid"></div>
<script type="text/javascript">
    require.config({
        baseUrl: '@Url.Content("~/Scripts")',
        paths: {
            'jquery': 'jquery-2.0.3.min',
            'kendo': 'kendo-ui',
            'kendo-mvc': 'kendo/kendo.aspnetmvc.min'
        },
        shim: {
            'jquery': {
                exports: 'jQuery'
            }
        }
    });
    require(['jquery', 'kendo-mvc', 'kendo/kendo.grid.min'], function ($) {
        $(document).ready(function () {
            $('#grid').kendoGrid({
                dataSource: {
                    schema: {
                        data: 'Data',
                        total: 'Total',
                        aggregates: 'AggregateResults',
                        model: {
                            id: "ProductId",
                            fields: {
                                ProductName: { type: "string" },
                                ProductPrice: { type: "number" }
                            }
                        }
                    },
                    transport: {
                        read: {
                            url: "@Url.Action("GetData", "Home")",
                            dataType: "json",
                            method: "post"
                        }
                    },
                    pageSize: 10,
                    serverPaging: true,
                    serverSorting: true,
                    type: "aspnetmvc-ajax"
                },
                sortable: {
                    mode: "single"
                },
                columns: ["ProductName", "ProductPrice"],
                scrollable: false,
                pageable: true
            });
        });
    });
</script>
</body>
</html>

但这产生了这个错误:

并尝试加载js文件:

红点是404未找到,因为它正在scripts文件夹下一个名为kendo的文件夹中寻找js文件。

然后我想我会尝试包含所有版本,所以我尝试了这个:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <link href="~/Content/kendo.common.min.css" rel="stylesheet" />
    <link href="~/Content/kendo.default.min.css" rel="stylesheet" />
    <script type="text/javascript" src="~/Scripts/require.js"></script>
</head>
<body>            
<div id="grid"></div>
<script type="text/javascript">
    require.config({
        baseUrl: '@Url.Content("~/Scripts")',
        paths: {
            'jquery': 'jquery-2.0.3.min',
            'kendo': 'kendo-ui/kendo.all.min',
            'kendo-mvc': 'kendo-ui/kendo.aspnetmvc.min'
        },
        shim: {
            'jquery': {
                exports: 'jQuery'
            }
        }
    });
    require(['jquery', 'kendo', 'kendo-mvc'], function ($) {
        $(document).ready(function () {
            $('#grid').kendoGrid({
                dataSource: {
                    schema: {
                        data: 'Data',
                        total: 'Total',
                        aggregates: 'AggregateResults',
                        model: {
                            id: "ProductId",
                            fields: {
                                ProductName: { type: "string" },
                                ProductPrice: { type: "number" }
                            }
                        }
                    },
                    transport: {
                        read: {
                            url: "@Url.Action("GetData", "Home")",
                            dataType: "json",
                            method: "post"
                        }
                    },
                    pageSize: 10,
                    serverPaging: true,
                    serverSorting: true,
                    type: "aspnetmvc-ajax"
                },
                sortable: {
                    mode: "single"
                },
                columns: ["ProductName", "ProductPrice"],
                scrollable: false,
                pageable: true
            });
        });
    });
</script>
</body>
</html>

但这产生了这些错误:

并尝试加载js文件:

在这种情况下 - 红点是 404 未找到,因为它正在直接在 Scripts 文件夹下查找文件。

所以这是我的问题:

如何在需要 JS 类型的场景中包含所述文件?

除此之外:我想使用 kendo.all.min 文件而不是单独的文件,因为我想在将来使用 knockout-kendo,这似乎不适用于单独的文件,但如果完成这项工作的唯一方法是使用单独的文件方法,这很好。

【问题讨论】:

标签: asp.net-mvc kendo-ui requirejs kendo-grid kendo-asp.net-mvc


【解决方案1】:

我花了一段时间才让你的代码正常工作,但在稍微摆弄了一下之后,我设法让排序工作,只需对你的原始代码进行一点点改动。

我唯一更改的是在我添加 mvc 文件的 require 行上。然后所有的路径都变得正确,一切都很好。

['jquery', 'kendo/kendo.grid.min', 'kendo/kendo.aspnetmvc.min']

在我的代码中,我使用了“Kendo UI for ASP.NET MVC Q2 2013”​​和包含在该包中的 jQuery.min.js 文件。那么完整的View代码就变成了:

<script type="text/javascript">            
  require.config({
    baseUrl : '@Url.Content("~/Scripts")',
    paths: {
        'jquery': 'jquery-2.0.3.min',
        'kendo': 'kendo-ui'
    },
    shim: {
        'jquery': {
            exports: 'jQuery'
        }
    }            
  });           
  require(['jquery', 'kendo/kendo.grid.min', 'kendo/kendo.aspnetmvc.min'], function ($) {
    $(document).ready(function () {
        $('#grid').kendoGrid({
            dataSource: {
                schema: {
                    data: 'Data',
                    total: 'Total',
                    aggregates: 'AggregateResults',
                    model: {
                        id: "ProductId",
                        fields: {
                            ProductName: { type: "string" },
                            ProductPrice: { type: "number" }
                        }
                    }
                },
                transport: {
                    read: {
                        url: "@Url.Action("GetData", "Home")",
                        dataType: "json",
                        method: "post"
                    }
                },
                pageSize: 10,
                serverPaging: true,
                serverSorting: true,
                type: "aspnetmvc-ajax"
            },
            sortable: {
                mode: "single"
            },
            columns: ["ProductName", "ProductPrice"],
            scrollable: false,
            pageable: true
        });
    });            
  });
</script>

我希望它也适用于您的代码。

【讨论】:

  • 谢谢 - 输入这段代码我得到两个 javascript 错误:jquery 的脚本错误和参考错误:jQuery 未定义
  • 那是因为我使用的 jQuery 在一个名为 jquery.min.js 的文件中。请在我的示例中将该行更改为您的 jquery 文件的名称。我应该对此更清楚。我也会更新我的示例,使其具有与您的相同的 jQuery 文件名。
  • 我还有剑道自带的jquery.min文件
  • 好吧,使用你的原始代码,忽略我的。然后只需在需要行上添加“kendo/kendo.aspnetmvc.min”...
  • 如果我的回答对你有用,那么请将我的回答也标记为正确:) 非常感谢...
【解决方案2】:

让我们尝试从最小的工作版本开始构建。您说您在目录中有以下内容:

  • Scripts/kendo-ui/*(所有 kendo 文件,包括 mvc 文件)
  • 脚本/require.js
  • 脚本/jquery-2.0.3.min.js

要让它加载所有依赖项,您可以尝试以下操作:

<html>
<body>
<script type="text/javascript" src="~/Scripts/require.js"></script>
<script type="text/javascript">
    require.config({
        baseUrl: '@Url.Content("~/Scripts")',
        paths: {
            'jquery': 'jquery-2.0.3.min',
            'kendo': 'kendo-ui/kendo.all.min',
            'kendo-mvc': 'kendo-ui/kendo.aspnetmvc.min'
        },
        shim: {
            'jquery': {
                exports: 'jQuery'
            },
            'kendo-mvc' : {
                deps: ['kendo'] //kendo needs to be loaded before kendo-mvc?
            }
        }
    });
    require(['jquery', 'kendo', 'kendo-mvc'], function ($) {
    });
</script>
</body>
</html>

我尝试将它放在jsFiddle 中,但遇到了许多问题(Kendo 实际上需要 jQuery 1.9.0 等),您可能可以自己解决。

关键似乎是您的上一个版本正在加载 kendo.data、kendo.combobox 和一堆其他任何地方都没有引用的文件。弄清楚这些请求的来源将有助于解开这个谜。

更新: 这是一种可能性。如果 kendo-mvc 正在加载这样的依赖项:

["./kendo.data.min","./kendo.combobox.min","./kendo.multiselect.min","./kendo.‌​validator.min"]

那么它可能会失败,因为 RequireJS 查找与模块名称相关的依赖项,该模块名称已别名为 kendo-mvc。让我们尝试不重命名它(见下文),看看是否可行:

<script type="text/javascript">
    require.config({
        baseUrl: '@Url.Content("~/Scripts")',
        paths: {
            'jquery': 'jquery-2.0.3.min',
            'kendo-ui/kendo': 'kendo-ui/kendo.all.min',
            'kendo-ui/kendo-mvc': 'kendo-ui/kendo.aspnetmvc.min'
        },
...
    require(['jquery', 'kendo-ui/kendo', 'kendo-ui/kendo-mvc'], function ($) {
    });

【讨论】:

  • 谢谢。使用您的方法,我仍然得到 404 not found forr kendo.data.min、combobox、multiselect 和验证器(与问题中的第二次尝试相同)。它正在直接在 Scripts 目录下查找所有 4 个文件。我打开了 kendo.aspnetmvc.min 文件,可以看到这个位 '(["./kendo.data.min","./kendo.combobox.min","./kendo.multiselect.min",". /kendo.validator.min"],function(){' 看起来像指定这些依赖项的定义。这对我来说有点混乱,因为文件与 kendo.aspnetmvc.min 位于同一目录中,因此 ./ 将能够找到他们:-S.
  • Anyhoo,是否有可能以某种方式配置它,使 ./xxx 指向 kendo-ui 下?
  • 尝试将 kendo.data.min 等添加到 require.config 中的路径。这些文件现在在哪里,kendo-ui?另外,您可以分享您正在使用的这些文件的副本吗?
  • 好的,我试试。你可以从这里下载所有剑道的东西:kendoui.com/download/… - 这是一个 30 天的试用许可证。
  • 哦,是的 - 所有这些文件现在都位于 kendo-ui 文件夹中
猜你喜欢
  • 2016-11-13
  • 1970-01-01
  • 1970-01-01
  • 2012-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多