Skip to content

Commit 4e38dfa

Browse files
petemillcrissdev
authored andcommitted
Use peek to check the existing rules
It prevents any encapsulating subscriptions to fire if the rules change. Closes #595
1 parent 8745eb1 commit 4e38dfa

7 files changed

Lines changed: 17 additions & 14 deletions

dist/knockout.validation-with-locales.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,12 @@ kv.configuration = configuration;
466466
//
467467
addRule: function (observable, rule) {
468468
observable.extend({ validatable: true });
469-
470-
var hasRule = !!koUtils.arrayFirst(observable.rules(), function(item) {
469+
//calculate if the observable already has this rule
470+
//peek the set of rules so this function does not cause any encapsulating subsciptions to fire if the rules change
471+
var hasRule = !!koUtils.arrayFirst(observable.rules.peek(), function(item) {
471472
return item.rule && item.rule === rule.rule;
472473
});
473-
474+
//do not add the rule if it already exists on the observable
474475
if (!hasRule) {
475476
//push a Rule Context to the observables local array of Rule Contexts
476477
observable.rules.push(rule);

dist/knockout.validation-with-locales.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/knockout.validation-with-locales.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/knockout.validation.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -466,11 +466,12 @@ kv.configuration = configuration;
466466
//
467467
addRule: function (observable, rule) {
468468
observable.extend({ validatable: true });
469-
470-
var hasRule = !!koUtils.arrayFirst(observable.rules(), function(item) {
469+
//calculate if the observable already has this rule
470+
//peek the set of rules so this function does not cause any encapsulating subsciptions to fire if the rules change
471+
var hasRule = !!koUtils.arrayFirst(observable.rules.peek(), function(item) {
471472
return item.rule && item.rule === rule.rule;
472473
});
473-
474+
//do not add the rule if it already exists on the observable
474475
if (!hasRule) {
475476
//push a Rule Context to the observables local array of Rule Contexts
476477
observable.rules.push(rule);

dist/knockout.validation.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/knockout.validation.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,12 @@
268268
//
269269
addRule: function (observable, rule) {
270270
observable.extend({ validatable: true });
271-
272-
var hasRule = !!ko.utils.arrayFirst(observable.rules(), function(item) {
271+
//calculate if the observable already has this rule
272+
//peek the set of rules so this function does not cause any encapsulating subsciptions to fire if the rules change
273+
var hasRule = !!ko.utils.arrayFirst(observable.rules.peek(), function(item) {
273274
return item.rule && item.rule === rule.rule;
274275
});
275-
276+
//do not add the rule if it already exists on the observable
276277
if (!hasRule) {
277278
//push a Rule Context to the observables local array of Rule Contexts
278279
observable.rules.push(rule);

0 commit comments

Comments
 (0)