Skip to content

Commit 74295c5

Browse files
committed
Simplify sfMessage directive and uses
There's no need to overthink sfMessage; in all cases it uses the parent scope's . This allows faster processing, and full data-binding so that an application can modify descriptions on-the-fly.
1 parent b4d01af commit 74295c5

File tree

10 files changed

+30
-55
lines changed

10 files changed

+30
-55
lines changed

docs/extending.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ This is sort of the template for the datepicker:
5454
max-date="form.maxDate"
5555
format="form.format" />
5656

57-
<span class="help-block" sf-message="form.description"></span>
57+
<span class="help-block" sf-message></span>
5858
</div>
5959
```
6060

src/directives/decorators/bootstrap/checkbox.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
name="{{form.key.slice(-1)[0]}}">
1212
<span ng-bind-html="form.title"></span>
1313
</label>
14-
<div class="help-block" sf-message="form.description"></div>
14+
<div class="help-block" sf-message></div>
1515
</div>

src/directives/decorators/bootstrap/checkboxes.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
</label>
1515

1616
</div>
17-
<div class="help-block" sf-message="form.description"></div>
17+
<div class="help-block" sf-message></div>
1818
</div>

src/directives/decorators/bootstrap/default.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,5 @@
5050
id="{{form.key.slice(-1)[0] + 'Status'}}"
5151
class="sr-only">{{ hasSuccess() ? '(success)' : '(error)' }}</span>
5252

53-
<div class="help-block" sf-message="form.description"></div>
53+
<div class="help-block" sf-message></div>
5454
</div>

src/directives/decorators/bootstrap/radio-buttons.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
<span ng-bind-html="item.name"></span>
2121
</label>
2222
</div>
23-
<div class="help-block" sf-message="form.description"></div>
23+
<div class="help-block" sf-message></div>
2424
</div>

src/directives/decorators/bootstrap/radios-inline.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
<span ng-bind-html="item.name"></span>
1515
</label>
1616
</div>
17-
<div class="help-block" sf-message="form.description"></div>
17+
<div class="help-block" sf-message></div>
1818
</div>

src/directives/decorators/bootstrap/radios.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,5 @@
1414
<span ng-bind-html="item.name"></span>
1515
</label>
1616
</div>
17-
<div class="help-block" sf-message="form.description"></div>
17+
<div class="help-block" sf-message></div>
1818
</div>

src/directives/decorators/bootstrap/select.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
ng-options="item.value as item.name group by item.group for item in form.titleMap"
1313
name="{{form.key.slice(-1)[0]}}">
1414
</select>
15-
<div class="help-block" sf-message="form.description"></div>
15+
<div class="help-block" sf-message></div>
1616
</div>

src/directives/decorators/bootstrap/textarea.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@
3131
ng-bind-html="form.fieldAddonRight"></span>
3232
</div>
3333

34-
<span class="help-block" sf-message="form.description"></span>
34+
<span class="help-block" sf-message></span>
3535
</div>

src/directives/message.js

+21-46
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,28 @@
11
angular.module('schemaForm').directive('sfMessage',
2-
['$injector', 'sfErrorMessage', function($injector, sfErrorMessage) {
2+
['sfErrorMessage', function(sfErrorMessage) {
33
return {
44
scope: false,
55
restrict: 'EA',
6-
link: function(scope, element, attrs) {
7-
8-
//Inject sanitizer if it exists
9-
var $sanitize = $injector.has('$sanitize') ?
10-
$injector.get('$sanitize') : function(html) { return html; };
11-
12-
//Prepare and sanitize message, i.e. description in most cases.
13-
var msg = '';
14-
if (attrs.sfMessage) {
15-
msg = scope.$eval(attrs.sfMessage) || '';
16-
msg = $sanitize(msg);
17-
}
18-
19-
var update = function(valid) {
20-
if (valid && !scope.hasError()) {
21-
element.html(msg);
22-
} else {
23-
24-
var errors = Object.keys(
25-
(scope.ngModel && scope.ngModel.$error) || {}
26-
);
27-
28-
// We only show one error.
29-
// TODO: Make that optional
30-
var error = errors[0];
31-
32-
if (error) {
33-
element.html(sfErrorMessage.interpolate(
34-
error,
35-
scope.ngModel.$modelValue,
36-
scope.ngModel.$viewValue,
37-
scope.form,
38-
scope.options && scope.options.validationMessage
39-
));
40-
} else {
41-
element.html(msg);
42-
}
43-
}
44-
};
45-
update();
46-
47-
scope.$watchCollection('ngModel.$error', function() {
48-
if (scope.ngModel) {
49-
update(scope.ngModel.$valid);
50-
}
6+
template: '<div ng-if="!error && form.description" ng-bind-html="form.description"></div><div ng-if="error" ng-bind-html="error"></div>',
7+
link: function(scope) {
8+
9+
scope.$watchCollection('ngModel.$error', function(errors) {
10+
11+
errors = Object.keys(errors);
12+
13+
// We only show one error.
14+
// TODO: Make that optional
15+
var error = errors[0];
16+
17+
scope.error = error ?
18+
sfErrorMessage.interpolate(
19+
error,
20+
scope.ngModel.$modelValue,
21+
scope.ngModel.$viewValue,
22+
scope.form,
23+
!!(scope.options && scope.options.validationMessage)
24+
)
25+
: null;
5126
});
5227

5328
}

0 commit comments

Comments
 (0)