@@ -159,8 +159,8 @@ angular.module('schemaForm').provider('schemaFormDecorators',
159
159
160
160
var createDirective = function ( name ) {
161
161
$compileProvider . directive ( name ,
162
- [ '$parse' , '$compile' , '$http' , '$templateCache' , '$interpolate' , 'sfErrorMessage' ,
163
- function ( $parse , $compile , $http , $templateCache , $interpolate , sfErrorMessage ) {
162
+ [ '$parse' , '$compile' , '$http' , '$templateCache' , '$interpolate' , '$q' , 'sfErrorMessage' ,
163
+ function ( $parse , $compile , $http , $templateCache , $interpolate , $q , sfErrorMessage ) {
164
164
165
165
return {
166
166
restrict : 'AE' ,
@@ -304,14 +304,28 @@ angular.module('schemaForm').provider('schemaFormDecorators',
304
304
//ok let's replace that template!
305
305
//We do this manually since we need to bind ng-model properly and also
306
306
//for fieldsets to recurse properly.
307
- var url = templateUrl ( name , form ) ;
308
- $http . get ( url , { cache : $templateCache } ) . then ( function ( res ) {
309
- var key = form . key ?
310
- sfPathProvider . stringify ( form . key ) . replace ( / " / g, '"' ) : '' ;
311
- var template = res . data . replace (
312
- / \$ \$ v a l u e \$ \$ / g,
313
- 'model' + ( key [ 0 ] !== '[' ? '.' : '' ) + key
314
- ) ;
307
+ var templatePromise ;
308
+
309
+ // type: "template" is a special case. It can contain a template inline or an url.
310
+ // otherwise we find out the url to the template and load them.
311
+ if ( form . type === 'template' && form . template ) {
312
+ templatePromise = $q . when ( form . template ) ;
313
+ } else {
314
+ var url = form . type === 'template' ? form . templateUrl : templateUrl ( name , form ) ;
315
+ templatePromise = $http . get ( url , { cache : $templateCache } ) . then ( function ( res ) {
316
+ return res . data ;
317
+ } ) ;
318
+ }
319
+
320
+ templatePromise . then ( function ( template ) {
321
+ if ( form . key ) {
322
+ var key = form . key ?
323
+ sfPathProvider . stringify ( form . key ) . replace ( / " / g, '"' ) : '' ;
324
+ template = template . replace (
325
+ / \$ \$ v a l u e \$ \$ / g,
326
+ 'model' + ( key [ 0 ] !== '[' ? '.' : '' ) + key
327
+ ) ;
328
+ }
315
329
element . html ( template ) ;
316
330
317
331
// Do we have a condition? Then we slap on an ng-if on all children,
@@ -995,11 +1009,15 @@ angular.module('schemaForm').provider('schemaForm',
995
1009
}
996
1010
997
1011
//extend with std form from schema.
998
-
999
1012
if ( obj . key ) {
1000
1013
var strid = sfPathProvider . stringify ( obj . key ) ;
1001
1014
if ( lookup [ strid ] ) {
1002
- obj = angular . extend ( lookup [ strid ] , obj ) ;
1015
+ var schemaDefaults = lookup [ strid ] ;
1016
+ angular . forEach ( schemaDefaults , function ( value , attr ) {
1017
+ if ( obj [ attr ] === undefined ) {
1018
+ obj [ attr ] = schemaDefaults [ attr ] ;
1019
+ }
1020
+ } ) ;
1003
1021
}
1004
1022
}
1005
1023
@@ -1750,6 +1768,28 @@ angular.module('schemaForm').directive('schemaValidate', ['sfValidator', 'sfSele
1750
1768
return viewValue ;
1751
1769
} ;
1752
1770
1771
+ // Custom validators, parsers, formatters etc
1772
+ if ( typeof form . ngModel === 'function' ) {
1773
+ form . ngModel ( ngModel ) ;
1774
+ }
1775
+
1776
+ [ '$parsers' , '$viewChangeListeners' , '$formatters' ] . forEach ( function ( attr ) {
1777
+ if ( form [ attr ] && ngModel [ attr ] ) {
1778
+ form [ attr ] . forEach ( function ( fn ) {
1779
+ ngModel [ attr ] . push ( fn ) ;
1780
+ } ) ;
1781
+ }
1782
+ } ) ;
1783
+
1784
+ [ '$validators' , '$asyncValidators' ] . forEach ( function ( attr ) {
1785
+ // Check if our version of angular has i, i.e. 1.3+
1786
+ if ( form [ attr ] && ngModel [ attr ] ) {
1787
+ angular . forEach ( form [ attr ] , function ( fn , name ) {
1788
+ ngModel [ attr ] [ name ] = fn ;
1789
+ } ) ;
1790
+ }
1791
+ } ) ;
1792
+
1753
1793
// Get in last of the parses so the parsed value has the correct type.
1754
1794
// We don't use $validators since we like to set different errors depeding tv4 error codes
1755
1795
ngModel . $parsers . push ( validate ) ;
0 commit comments