我将使用先前提出的问题的代码进行细微更改,但情况不同:
HTML:
<button onclick="openPopUp()">OpenPopUp</button>
<div id="productionStates" class="hidden">
<div data-bind="kendoWindow: { isOpen: isOpen, title:'States', center:true, width: 600, height: 150, modal: true, resizable: false, actions: ['Maximize', 'Close'] }" >
<fieldset>
<legend>Change state:</legend>
<table>
<tr data-bind="foreach: productionStates">
<td><button class="k-button" data-bind="value: ProductionState, text: ProductionState" /></td>
</tr>
</table>
</fieldset>
</div>
</div>
javascript:
var ProductionStatesList = function() {
var self = this;
ProductionStatesList.prototype.productionStates =
ko.observableArray([ { ProductionState: ko.observable("Pending") } ]);
ProductionStatesList.prototype.openPopUp = function () {
self.isOpen(true);
};
ProductionStatesList.prototype.isOpen = ko.observable(false);
ProductionStatesList.prototype.close = function () {
self.isOpen(false);
}
};
var elementIsBound = function (elementId) {
return !!ko.dataFor(document.getElementById(elementId));
};
var myProductionStatesList = ko.observable();
var openPopUp = function(){
myProductionStatesList(new ProductionStatesList());
if (!elementIsBound("productionStates")){
ko.applyBindings(myProductionStatesList, document.getElementById("productionStates"));
}
myProductionStatesList().openPopUp();
}
myProductionStatesList
是一个可观察的。单击按钮弹出窗口打开,我正在实例化ProductionStatesList
视图模型并将其值分配给myProductionStatesList
. 第一次单击该按钮时,一切正常。当您关闭弹出窗口并再次单击按钮时,绑定被破坏并且没有任何反应。我希望在每次单击按钮时,ProductionStatesList 的新实例都会像myProductionStatesList
可观察的那样反弹。我错过了什么?
jsfiddle: