我正在使用 RPNiemeyer kendo-knockout 库。我有一个网格。当用户单击一行网格时,会显示一个弹出窗口。当您关闭窗口并以同样的方式再次打开它时,应用程序会随着关闭动画冻结。我尽我所能在小提琴中重现这个场景。当您关闭弹出窗口并再次单击该行时,没有任何反应并重新加载浏览器。我坚信我的应用程序中正在发生类似的事情。
html:
<div data-viewId="languageList" >
<div id="languageList" data-bind="with: viewModel">
<div id="languageListGrid" data-bind="kendoGrid: { data: languageViewModels, columns: [
{
template: '<a href=\'\' data-bind=\'click: function() { onLanguageSelected("#=Language#") }\'>#=Language#</a>',
field: 'Language',
title: 'Language',
width: 50
}
],
scrollable: false, sortable: true, pageable: false }" style="height: 380px">
</div>
</div>
</div>
<div data-viewid="languageDetails">
<div id="languageDetails" data-bind="with: viewModel" class="hidden">
<form id="languageDetailsForm" action="" style="font-family: Trebuchet MS, Verdana, Helvetica, Sans-Serif;">
<div data-bind="kendoWindow: {isOpen: isOpen, title:'Language', width: 400, height: 200, modal: true }" >
test
<button id="cancelLanguage" class="k-button" data-bind="click: cancelLanguage">Cancel</button>
</div>
</form>
</div>
</div>
javascript:
$(function () {
var elementIsBoundNew = function (element) {
return !!ko.dataFor(element);
}
var applyBindings = function (viewModel, elementId) {
var element = $('div[data-viewId="' + elementId + '"]')[0];
if (!elementIsBoundNew(element)) {
var parentViewModel = { viewModel: viewModel };
ko.applyBindings(parentViewModel, element);
}
};
var FranchiseDetailsViewModel = function () {
var
self = this,
initialize = function () {
self.languagesInfoViewModel(new LanguageListViewModel(self));
applyBindings(self.languagesInfoViewModel, "languageList");
};
FranchiseDetailsViewModel.prototype.languagesInfoViewModel = ko.observable();
initialize();
};
var LanguageListViewModel = function (franchise) {
var
self = this,
initialize = function () {
var languageViewModel = new LanguageDetailsViewModel(franchise);
self.languageViewModels.push(languageViewModel);
};
LanguageListViewModel.prototype.languageViewModels = ko.observableArray([]);
LanguageListViewModel.prototype.selectedLanguageViewModel = ko.observable();
LanguageListViewModel.prototype.onLanguageSelected = function (selectedLanguage) {
// when you uncomment this line everyting works fine
//var language = new LanguageDetailsViewModel();
self.selectedLanguageViewModel(self.languageViewModels()[0]);
applyBindings(self.selectedLanguageViewModel, "languageDetails");
self.selectedLanguageViewModel().openPopUp();
};
initialize();
};
var LanguageDetailsViewModel = function () {
var
self = this,
closePopUp = function () {
self.isOpen(false);
};
self.Language = ko.observable("English");
LanguageDetailsViewModel.prototype.isOpen = ko.observable(false);
LanguageDetailsViewModel.prototype.openPopUp = function () {
self.isOpen(true);
};
LanguageDetailsViewModel.prototype.cancelLanguage = function () {
closePopUp();
};
};
var initialize = new FranchiseDetailsViewModel();
});
奇怪的是,如果我将这行代码添加到我的 onLanguageSelected 方法中,一切都可以正常工作:
var language = new LanguageDetailsViewModel();
小提琴:
任何有关工作示例的帮助将不胜感激。谢谢!
根据 RPNiemeyer 的帖子更新:
我已经添加了这些代码行以使用此处Kendo-Knockout 中的技术:调用一个方法从模板中更改 viewmodel 属性,并在网格内进行数据绑定,破坏绑定:
ko.bindingHandlers.preventBinding = {
init: function() {
return { controlsDescendantBindings: true };
}
};
ko.bindingHandlers.kendoGrid.options.dataBound = function(data) {
var body = this.element.find("tbody")[0];
if (body) {
ko.applyBindings(ko.dataFor(body), body);
}
};
这正是我的应用程序中正在发生的事情。当我打开弹出窗口时,将其关闭,然后再次打开它,它没有正确关闭。请看我更新的小提琴:
我错过了什么?再次感谢您的反馈!