我正在测试 Kendo UI 的单页应用程序 (SPA) 和 MVVM 功能,但是在将 Viewmodel 绑定到作为页面内容的列表视图时遇到了一些问题......
到目前为止,我所拥有的是:
<div id="app">
<button data-bind="click: gotopage1">Page 1</button>
<button data-bind="click: gotopage2">Page 2</button>
<button data-bind="click: gotopage3">Page 3</button>
</div>
<script id="page1" type="text/x-kendo-template">
<ul id="listView1" data-bind="source: photossource"></ul>
</script>
<script id="page2" type="text/x-kendo-template">
//content of page 2
</script>
<script id="page3" type="text/x-kendo-template">
//content of page 3
</script>
<script id="layout" type="text/x-kendo-template">
<header></header><section id=content></section><footer></footer>
</script>
<script type="text/x-kendo-template" id="templatelistitem">
<div class="item">
<img data-bind="attr: { src: this }" />
</div>
</script>
<script>
var set1 = new Array();
var set2 = new Array();
var set3 = new Array();
//fill the arrays... they are just strings to put on the `src` attribute of the `img`
var appViewModel = new kendo.observable({
gotopage1: function () {
router.navigate("/");
},
gotopage2: function () {
router.navigate("/page2");
},
gotopage3: function () {
router.navigate("/page3");
}
});
kendo.bind($("#app"), appViewModel);
var pageViewModel = new kendo.observable({
photossource: set1
});
var page1 = new kendo.View("#page1");
var page2 = new kendo.View("#page2");
var page3 = new kendo.View("#page3");
var layout = new kendo.Layout("#layout");
var router = new kendo.Router();
router.route("/", function () {
pageViewModel.photossource = set1;
layout.showIn("#content", page1);
});
router.route("/page2", function () {
pageViewModel.photossource = set2;
layout.showIn("#content", page2);
});
router.route("/page3", function () {
pageViewModel.photossource = set3;
layout.showIn("#content", page3);
});
$(function () {
router.start();
layout.render($("#app"));
layout.showIn("#content", page1);
});
$(document).ready(function () {
$("#listView1").kendoListView({
template: kendo.template($("#templatelistitem").html())
});
kendo.bind($("#listView1"), pageViewModel);
});
</script>
我需要绑定pageViewModel
到listview1
page1。这pageViewModel
将在 3 个页面上共享。
这给了我以下错误:
未捕获的类型错误:无法读取
kendo.web.min.js:12中未定义的属性“父级”
我的主要问题是:
我应该如何将视图模型绑定到列表视图?
我需要设置列表视图的数据源吗?
我如何
photossource
在列表项的模板中引用?