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

Commit 906fdef

Browse files
committed
Merge pull request #39 from kentcdodds/master
Adding watch property to fields
2 parents 5368a1e + c202ebc commit 906fdef

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Adding the `formlyOptionsProvider` which allows developers to set default form o
88

99
Adding `submitButtonTemplate` to the configurable `formlyOptions`. If it has a value, then the button in the `formly-form` directive will be replaced with the compiled (on the scope) version of that value. Also adding twitter classes to the submit button so hopefully this template will be unecessary in some cases.
1010

11+
Adding the `watch` property which has both `expression` and `listener` properties. Formly will set a watcher on its own scope. If the `watch.expression` is a function, it will be wrapped and the first argument to that expression function will be the field, the rest will be the normal watch expression definition. The same is true for `listener`.
12+
1113
## Bug Fixes
1214

1315
## Breaking Changes

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,23 @@ When constructing fields use the options below to customize each field object. Y
178178
###### Default
179179
>`undefined`
180180
181+
---
182+
##### watch.expression (object)
183+
>`watch` has two properties called `expression` and `listener`. The `watch.expression` is added to the formly directive's scope. If it's a function, it will be wrapped and called with the field as the first argument, followed by the normal arguments for a watcher. The `listener` will also be wrapped and called with the field as the first argument, followed by hte normal arguments for a watch listener.
184+
185+
For example:
186+
187+
```javascript
188+
// normal watcher
189+
$scope.$watch(function expression(theScope) {}, function listener(newValue, oldValue, theScope) {});
190+
191+
// field watcher
192+
$scope.$watch(function expression(field, theScope) {}, function listener(field, newValue, oldValue, theScope) {});
193+
```
194+
195+
###### Default
196+
>`undefined`
197+
181198
### Form Fields
182199
Below is a detailed description of each form fields and its custom properties.
183200

src/directives/formly-form.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,31 @@ angular.module('formly.render')
2525
}
2626
},
2727
controller: function($scope, $element, $parse) {
28-
$scope.$watch('result', function(newValue) {
28+
// setup watches for watchExpressions
2929
angular.forEach($scope.fields, function(field, index) {
30+
if (angular.isDefined(field.watch)) {
31+
var watchExpression = field.watch.expression;
32+
if (angular.isFunction(watchExpression)) {
33+
// wrap the field's watch expression so we can call it with the field as the first arg as a helper
34+
watchExpression = function() {
35+
var args = Array.prototype.slice.call(arguments, 0);
36+
args.unshift(field);
37+
return field.watch.expression.apply(this, args);
38+
}
39+
}
40+
41+
$scope.$watch(watchExpression, function() {
42+
// wrap the field's watch listener so we can call it with the field as the first arg as a helper
43+
var args = Array.prototype.slice.call(arguments, 0);
44+
args.unshift(field);
45+
return field.watch.listener.apply(this, args);
46+
});
47+
}
48+
});
49+
$scope.$watch('result', function(newValue) {
50+
angular.forEach($scope.fields, function(field, index) {
3051
if (field.hideExpression) {
31-
var getter = $parse(field.hideExpression);
32-
field.hide = getter($scope.result);
52+
field.hide = $parse(field.hideExpression)($scope.result);
3353
}
3454
});
3555
}, true);

src/views/home.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ <h3>Form</h3>
6868
options="formOptions"
6969
ng-submit="onSubmit()">
7070
</formly-form>
71+
<br />
72+
<br />
73+
<formly-form name="hiddenFormlyForm"
74+
result="hiddenFormData"
75+
fields="hiddenFormFields"
76+
options="hiddenFormOptions">
7177
</div>
7278
<div class="col-sm-4 col-md-4 col-lg-4">
7379

src/views/home.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,35 @@ app.controller('home', function($scope, $parse, formlyOptions, $window, usingCus
175175
default: 'secret_code'
176176
}];
177177

178+
$scope.hiddenFormFields = [
179+
{
180+
key: 'field',
181+
type: 'textarea',
182+
label: 'This is a special form field',
183+
placeholder: 'It has a watch property with an expression function that depends on something outside the result...',
184+
watch: {
185+
expression: function(field) {
186+
return !/joe/ig.test($scope.formData.hiddenWhenUnchecked);
187+
},
188+
listener: function(field, _new) {
189+
field.hide = _new;
190+
}
191+
}
192+
}
193+
];
194+
178195
$scope.formOptions = {
179196
uniqueFormId: 'formly',
180197
submitCopy: 'Save',
181198
hideSubmit: false
182199
};
200+
$scope.hiddenFormOptions = {
201+
uniqueFormId: 'hiddenFormly',
202+
hideSubmit: true
203+
};
183204
$scope.submittedData = null;
184205
$scope.formData = {};
206+
$scope.hiddenFormData = {};
185207
$scope.formFieldsStr = $scope.toPrettyJSON($scope.formFields, 4);
186208
$scope.formOptionsStr = $scope.toPrettyJSON($scope.formOptions, 4);
187209
$scope.formFieldsError = false;

0 commit comments

Comments
 (0)