Kendo UI MVVM - 如何在视图中迭代并呈现集合?

时间:2015-05-04 22:18:38

标签: javascript html kendo-ui kendo-mvvm client-side-templating

我只是试图在我的Kendo.View中循环数组并尝试从元素中呈现属性。这在MVC Razor中会非常简单,例如

@foreach( var displayLink in Model ) {
 <h1>displayLink.Text</h1>
}

而不是选择摘录,我只是分享了整个文件。

这一切都是运行,没有例外等。视图呈现静态内容但不呈现循环的内容。我打开了evalTemplate = true,但仍然没有骰子。我还没有找到办法做到这一点,这让我疯了。我能找到的就是连接Kendo UI ListView等的方法。我不想要那个重量,我只是想直接在数组上循环。

Index.htm(查看):

<div class="jumbotron">
    <div class="container">
        <h1>Web</h1>
        <p>The future is <i>now</i>.
        </p>
    </div>
</div>


# for(var i = 0; i < DashboardLinks.length; i++) { #
    <h1>#= DashboardLinks[i].TitleText #</h1>
# } #

控制器:

define(
    // == INTERFACE NAME ==
    "Controllers.IHome", 

     // == DEPENDENCIES ==
    [
        "Util.IGetViewSource", 
        "Util.ILayout",
        "ViewModels.Home.IHomeVM"
    ],

    function ( /* Dependency injections: */ getViewSource, layout, iHomeVM)
    {

        // Define the module.
        var module =
           {
               index: function () {

                   getViewSource("~/App/Views/Home/Index.htm", function (viewSource) {
                       // get the model
                       var viewModel = new iHomeVM();
                       viewModel.AddDashboardLink("#timecard", "Time Cards", "Manage time cards and get it done.", "time");

                       // render the view
                       var view = new kendo.View(viewSource, { model: viewModel, evalTemplate: true });

                       // render the view
                       layout.renderBodyView(view);
                   });
               }
           };

        // Return the module.
        return module;
    }
);

HomeVM:

define(
    // == INTERFACE NAME ==
    "ViewModels.Home.IHomeVM",

    // == DEPENDENCIES ==
    [
        "ViewModels.Shared.ILinkVM"
    ],
    function(
        // == DEPENDENCY INJECTIONS ==
        iLinkVM
    ) {
        // == CONSTRUCTOR ==
        function HomeVM() {
            console.log("HomeVM constructor executing.");


            // == PROPERTIES & METHODS ==
            this.DashboardLinks = [];


            // Return a copy of this wrapped in Kendo's observable.
            return kendo.observable(this);
        }

        HomeVM.prototype.AddDashboardLink = function(
            href,
            titleText,
            descriptionText,
            iconName) {
            this.DashboardLinks.push(new iLinkVM(
                href,
                titleText,
                descriptionText,
                iconName
            ));
        } 

        // Return the view model module.
        return HomeVM;
    }
);

LinkVM:

define(
    // == INTERFACE NAME ==
    "ViewModels.Shared.ILinkVM",  

    // == DEPENDENCIES ==
    [

    ],

    function (
        // == DEPENDENCY INJECTIONS ==

    )
    {
        // == CONSTRUCTOR ==
        function LinkVM(href, titleText, descriptionText, iconName) {
            console.log("LinkVM constructor executing.");


            // == PROPERTIES & METHODS ==
            this.Href = href;
            this.TitleText = titleText;
            this.DescriptionText = descriptionText;
            this.IconName = iconName;


            // Return a copy of this wrapped in Kendo's observable.
            return kendo.observable(this);
        }


        // Return the view model module.
        return LinkVM;
    }
);

2 个答案:

答案 0 :(得分:1)

我找到了:您可以通过设置“evalTemplate”属性来执行此操作: http://docs.telerik.com/kendo-ui/api/javascript/view#configuration-evalTemplate

  // create the view
  var view = new kendo.View(viewSource, { model: viewModel, evalTemplate: true });

然后您可以使用MVVM声明性绑定以及Kendo模板绑定,例如for循环。

确保你正确地逃避所有哈希('#'),否则模板会爆炸。

答案 1 :(得分:0)

乍看之下,您似乎错过了剑道模板的一些步骤。具体而言,您需要在<script type="text/x-kendo-template">标记中定义模板并将数据传递给它。对于我正在制作的例子,这将表示为

<script id="myTemplate" type="text/x-kendo-template">
    # for (var i = 0; i < data.DashboardLinks.length; i++) { #
        <h1>#= data.DashboardLinks[i].TitleText #</h1>
    # } #
</script>

然后为了使用它,我们可以利用这些Kendo函数动态渲染我们的模板 - 我们可以将其注入我们的DOM

var template = kendo.template($("#myTemplate").html()); // notice id on <script>

var data = { DashboardLinks: [{TitleText : 'LinkA'}, {TitleText: 'LinkB'}, {TitleText: 'LinkC'}] };

var result = template(data);

注意我们如何将data传递给template(data)。此函数呈现我们的模板,实际上如果我们console.log(result)此时我们看到了渲染模板

  

&LT; H1&GT; LinkA的&LT; / H1&GT;
  &LT; H1&GT; LinkB上&LT; / H1&GT;
  &LT; H1&GT; LinkC&LT; / H1&GT;

然后可以调用.html(result)将我们呈现的标记注入我们的DOM

来源:Kendo UI Templates Overview

工作示例:Kendo UI Dojo