Search code examples
knockout.jsknockout-validation

Knockout Validation with throttle


I just noticed that using the throttle extender causes Knockout Validation to stop working. Is there a way around this problem?

var viewModel = {
    label1: ko.observable('label1').extend({required: true}),
    label2: ko.observable('label2').extend({required: true, throttle: 1}),
};

ko.applyBindings(viewModel);

jsFiddle: http://jsfiddle.net/rWqkC/


Solution

  • In this case the order of the extenders does matter because the throttle extender return a new ko.dependentObservable that's why if you have the required first then it will apply on the wrong observable.

    Change the order and it should work:

    ko.observable('label2').extend({throttle: 500, required: true }),
    

    But because the extender execution in the order of the property declaration isn't really defined you are safer if you use use two extends in this case:

    ko.observable('label2').extend({throttle: 500}).extend({required: true })
    

    Demo fiddle.