And*_*ren 5 html javascript client-side-templating kendo-ui kendo-mvvm
我只是想遍历Kendo.View中的数组,并尝试从元素中渲染属性。这在MVC Razor中将非常简单,例如
@foreach( var displayLink in Model ) {
<h1>displayLink.Text</h1>
}
Run Code Online (Sandbox Code Playgroud)
与其选择摘录,我只是共享了整个文件。
所有这些都会运行,没有异常等。该视图呈现静态内容,但不呈现循环的内容。我打开了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>
# } #
Run Code Online (Sandbox Code Playgroud)
控制器:
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;
}
);
Run Code Online (Sandbox Code Playgroud)
家用VM:
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;
}
);
Run Code Online (Sandbox Code Playgroud)
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;
}
);
Run Code Online (Sandbox Code Playgroud)
我找到了:您可以通过设置“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 });
Run Code Online (Sandbox Code Playgroud)
然后您可以使用 MVVM 声明式绑定以及 Kendo 模板绑定,例如 for 循环。
确保正确转义所有哈希值(“#”),否则模板将会崩溃。