Skip to content

Commit e7ff94b

Browse files
committed
Watch description changes
After discussion in json-schema-form#387
1 parent 38135b4 commit e7ff94b

File tree

3 files changed

+67
-6
lines changed

3 files changed

+67
-6
lines changed

karma.conf.js

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module.exports = function(config) {
2929
'test/services/decorators-test.js',
3030
'test/services/messages-test.js',
3131
'test/directives/schema-form-test.js',
32+
'test/directives/sf-messages-test.js',
3233
],
3334

3435
// list of files to exclude

src/directives/message.js

+14-6
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,24 @@ angular.module('schemaForm').directive('sfMessage',
99
var $sanitize = $injector.has('$sanitize') ?
1010
$injector.get('$sanitize') : function(html) { return html; };
1111

12-
//Prepare and sanitize message, i.e. description in most cases.
13-
var msg = '';
12+
var message = '';
13+
1414
if (attrs.sfMessage) {
15-
msg = scope.$eval(attrs.sfMessage) || '';
16-
msg = $sanitize(msg);
15+
scope.$watch(attrs.sfMessage, function(msg) {
16+
if (msg) {
17+
message = $sanitize(msg);
18+
if (scope.ngModel) {
19+
update(scope.ngModel.$valid);
20+
} else {
21+
update();
22+
}
23+
}
24+
});
1725
}
1826

1927
var update = function(valid) {
2028
if (valid && !scope.hasError()) {
21-
element.html(msg);
29+
element.html(message);
2230
} else {
2331

2432

@@ -48,7 +56,7 @@ angular.module('schemaForm').directive('sfMessage',
4856
scope.options && scope.options.validationMessage
4957
));
5058
} else {
51-
element.html(msg);
59+
element.html(message);
5260
}
5361
}
5462
};

test/directives/sf-messages-test.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
chai.should();
2+
3+
describe('directive',function() {
4+
beforeEach(module('templates'));
5+
beforeEach(module('schemaForm'));
6+
beforeEach(
7+
//We don't need no sanitation. We don't need no thought control.
8+
module(function($sceProvider){
9+
$sceProvider.enabled(false);
10+
})
11+
);
12+
13+
it('should watch description for changes', function(done) {
14+
15+
var exampleSchema = {
16+
"type": "object",
17+
"properties": {
18+
"name": {
19+
"title": "Name",
20+
"type": "string"
21+
}
22+
}
23+
};
24+
25+
inject(function($compile,$rootScope) {
26+
var scope = $rootScope.$new();
27+
scope.person = {};
28+
29+
scope.schema = exampleSchema;
30+
31+
scope.form = [{
32+
key: 'name',
33+
description: 'foobar'
34+
}];
35+
36+
var tmpl = angular.element('<form sf-schema="schema" sf-form="form" sf-model="person"></form>');
37+
38+
$compile(tmpl)(scope);
39+
$rootScope.$apply();
40+
console.log(tmpl.children().find('div.help-block')[0]);
41+
tmpl.children().find('div.help-block').text().should.equal('foobar');
42+
43+
setTimeout(function() {
44+
scope.form[0].description = 'changed';
45+
scope.$apply();
46+
tmpl.children().find('div.help-block').text().should.equal('changed');
47+
done();
48+
},0);
49+
50+
});
51+
});
52+
});

0 commit comments

Comments
 (0)