0

我将使用先前提出的问题的代码进行细微更改,但情况不同:

Kendo-Knockout:关闭窗口会破坏绑定

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:

http://jsfiddle.net/bZF9k/15/

4

1 回答 1

2

我认为在这种情况下你最好的选择是达到你不需要ko.applyBindings多次跟注的地步。

一个不错的选择是创建一个视图模型,该模型具有一个可观察的来保存您的当前ProductionStatesList和您的openPopup功能。然后,with在编辑器周围使用绑定。

视图模型可能看起来像:

var viewModel = {
    myProductionStatesList: ko.observable(),
    openPopup: function() {
       var newList = new ProductionStatesList();
       this.myProductionStatesList(newList);
       newList.openPopup();
    }
};

这是一个示例:http: //jsfiddle.net/rniemeyer/wBCdK/

于 2012-12-17T13:44:35.820 回答