【问题标题】:Kendo-Knockout: bindings break when changing the value of the bind elementKendo-Knockout:更改绑定元素的值时绑定中断
【发布时间】:2012-12-17 11:08:28
【问题描述】:

我将使用先前提出的问题的代码,稍作改动,但情况不同:

Kendo-Knockout: Closing window breaks bindings

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/

【问题讨论】:

    标签: knockout.js kendo-ui


    【解决方案1】:

    我认为在这种情况下你最好的选择是达到你不需要多次调用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/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-11
      • 2014-05-05
      • 2014-03-03
      • 2012-12-24
      • 2015-07-24
      • 2013-05-04
      • 2012-03-16
      • 2017-08-01
      相关资源
      最近更新 更多