File tree 3 files changed +67
-6
lines changed
3 files changed +67
-6
lines changed Original file line number Diff line number Diff line change @@ -29,6 +29,7 @@ module.exports = function(config) {
29
29
'test/services/decorators-test.js' ,
30
30
'test/services/messages-test.js' ,
31
31
'test/directives/schema-form-test.js' ,
32
+ 'test/directives/sf-messages-test.js' ,
32
33
] ,
33
34
34
35
// list of files to exclude
Original file line number Diff line number Diff line change @@ -9,16 +9,24 @@ angular.module('schemaForm').directive('sfMessage',
9
9
var $sanitize = $injector . has ( '$sanitize' ) ?
10
10
$injector . get ( '$sanitize' ) : function ( html ) { return html ; } ;
11
11
12
- //Prepare and sanitize message, i.e. description in most cases.
13
- var msg = '' ;
12
+ var message = '' ;
13
+
14
14
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
+ } ) ;
17
25
}
18
26
19
27
var update = function ( valid ) {
20
28
if ( valid && ! scope . hasError ( ) ) {
21
- element . html ( msg ) ;
29
+ element . html ( message ) ;
22
30
} else {
23
31
24
32
@@ -48,7 +56,7 @@ angular.module('schemaForm').directive('sfMessage',
48
56
scope . options && scope . options . validationMessage
49
57
) ) ;
50
58
} else {
51
- element . html ( msg ) ;
59
+ element . html ( message ) ;
52
60
}
53
61
}
54
62
} ;
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments