Skip to content

Commit

Permalink
Use peek to check the existing rules
Browse files Browse the repository at this point in the history
It prevents any encapsulating subscriptions to fire if the rules change.

Closes #595
  • Loading branch information
petemill authored and crissdev committed Dec 8, 2015
1 parent 8745eb1 commit 4e38dfa
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 14 deletions.
7 changes: 4 additions & 3 deletions dist/knockout.validation-with-locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,12 @@ kv.configuration = configuration;
//
addRule: function (observable, rule) {
observable.extend({ validatable: true });

var hasRule = !!koUtils.arrayFirst(observable.rules(), function(item) {
//calculate if the observable already has this rule
//peek the set of rules so this function does not cause any encapsulating subsciptions to fire if the rules change
var hasRule = !!koUtils.arrayFirst(observable.rules.peek(), function(item) {
return item.rule && item.rule === rule.rule;
});

//do not add the rule if it already exists on the observable
if (!hasRule) {
//push a Rule Context to the observables local array of Rule Contexts
observable.rules.push(rule);
Expand Down
4 changes: 2 additions & 2 deletions dist/knockout.validation-with-locales.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/knockout.validation-with-locales.min.js.map

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions dist/knockout.validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,11 +466,12 @@ kv.configuration = configuration;
//
addRule: function (observable, rule) {
observable.extend({ validatable: true });

var hasRule = !!koUtils.arrayFirst(observable.rules(), function(item) {
//calculate if the observable already has this rule
//peek the set of rules so this function does not cause any encapsulating subsciptions to fire if the rules change
var hasRule = !!koUtils.arrayFirst(observable.rules.peek(), function(item) {
return item.rule && item.rule === rule.rule;
});

//do not add the rule if it already exists on the observable
if (!hasRule) {
//push a Rule Context to the observables local array of Rule Contexts
observable.rules.push(rule);
Expand Down
2 changes: 1 addition & 1 deletion dist/knockout.validation.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/knockout.validation.min.js.map

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,12 @@
//
addRule: function (observable, rule) {
observable.extend({ validatable: true });

var hasRule = !!ko.utils.arrayFirst(observable.rules(), function(item) {
//calculate if the observable already has this rule
//peek the set of rules so this function does not cause any encapsulating subsciptions to fire if the rules change
var hasRule = !!ko.utils.arrayFirst(observable.rules.peek(), function(item) {
return item.rule && item.rule === rule.rule;
});

//do not add the rule if it already exists on the observable
if (!hasRule) {
//push a Rule Context to the observables local array of Rule Contexts
observable.rules.push(rule);
Expand Down

0 comments on commit 4e38dfa

Please sign in to comment.