Change event on select with knockout binding, how can I know if it is a real change?

前端 未结 11 1880
独厮守ぢ
独厮守ぢ 2021-01-30 08:05

I am building a permissions UI, I have a list of permissions with a select list next to each permission. The permissions are represented by an observable array of objects which

11条回答
  •  隐瞒了意图╮
    2021-01-30 08:26

    Quick and dirty, utilizing a simple flag:

    var bindingsApplied = false;
    
    var ViewModel = function() {
        // ...
    
        this.permissionChanged = function() {
            // ignore, if flag not set
            if (!flag) return;
    
            // ...
        };
    };
    
    ko.applyBindings(new ViewModel());
    bindingsApplied = true; // done with the initial population, set flag to true
    

    If this doesn't work, try wrapping the last line in a setTimeout() - events are async, so maybe the last one is still pending when applyBindings() already returned.

提交回复
热议问题