Skip to content
This repository was archived by the owner on Apr 30, 2018. It is now read-only.

Commit 414cc34

Browse files
author
Kent C. Dodds
committed
Improved performance for cases where a single expression is used for multiple fields. Also improved demo
1 parent af2303f commit 414cc34

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/directives/formly-form.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,23 @@ angular.module('formly.render')
1717
//Post gets called after angular has created the FormController
1818
//Now pass the FormController back up to the parent scope
1919
scope.formOnParentScope = scope[attr.name];
20+
21+
var watches = {};
2022
angular.forEach(scope.fields, function(field) {
2123
if (field.hideExpression) {
22-
var getter = $parse(field.hideExpression);
24+
watches[field.hideExpression] = watches[field.hideExpression] || [];
25+
watches[field.hideExpression].push(field);
26+
}
27+
});
28+
angular.forEach(watches, function(fields, expression) {
29+
var getter = $parse(expression);
2330
scope.$watch(function() {
2431
return getter(scope.result);
2532
}, function(hide) {
26-
field.hide = hide;
33+
angular.forEach(fields, function(field) {
34+
field.hide = hide;
35+
});
2736
});
28-
}
2937
});
3038
}
3139
}

src/views/home.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,20 @@ app.controller('home', function($scope, $parse, $rootScope) {
147147
key: 'hiddenWhenUnchecked',
148148
type: 'text',
149149
label: 'Conditional input',
150+
placeholder: 'These both use the same hideExpression...',
150151
hideExpression: '!checkThis'
152+
}, {
153+
key: 'hiddenWhenUnchecked2',
154+
type: 'text',
155+
label: 'Type "joe" in here',
156+
placeholder: '... so there is only one $watch for both of them',
157+
hideExpression: '!checkThis'
158+
}, {
159+
key: 'showWhenJoe',
160+
type: 'text',
161+
label: 'You typed Joe! You found me!',
162+
placeholder: 'Different hideExpression, different $watch',
163+
hideExpression: 'hiddenWhenUnchecked2 !== "joe"'
151164
}, {
152165
key:'secretCode',
153166
type: 'hidden',

0 commit comments

Comments
 (0)