我创建了一个局部视图,可以将其包含在其他视图中以处理通知。
<div id="notifications">
<!-- ko with: $root.currentNotification -->
<span data-bind="text: notificationType"></span>
<span data-bind="text: friendlyMessage"></span>
<!-- ko if: $root.technicalMessageShown -->
<span data-bind="text: technicalMessage"></span>
<!-- /ko -->
<button data-bind="click: $root.toggleTechnicalMessage, text: $root.technicalMessageButtonText"></button>
<button data-bind="click: $root.dismissNotification">Dismiss</button>
<!-- /ko -->
<pre data-bind="text: ko.toJSON($root, null, 2)"></pre>
</div>
使用父视图中的以下绑定将视图绑定到视图模型。
var notificationsViewModel = new NotificationsViewModel();
ko.applyBindings(notificationsViewModel, document.getElementById("notifications"));
var viewModel = new MainViewModel();
ko.applyBindings(viewModel);
渲染时,'ko with: $root.currentNotification' 中的元素不包含在 DOM 中,即使 currentNotification 存在于视图模型上。
从局部视图的最后一行显示的视图模型:
{
"allNotifications": [
{
"id": "0",
"notificationType": "Error",
"friendlyMessage": "Lorem ipsum dolor sit amet.",
"technicalMessage": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque elit nulla, porta ac condimentum in, condimentum sed dui. Aliquam interdum."
}
],
"currentNotification": {
"id": "0",
"notificationType": "Error",
"friendlyMessage": "Lorem ipsum dolor sit amet.",
"technicalMessage": "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque elit nulla, porta ac condimentum in, condimentum sed dui. Aliquam interdum."
},
"technicalMessageButtonText": "More Info"
}
知道为什么淘汰赛没有看到当前的通知吗?