@@ -10,7 +10,7 @@ angular.module('ui.date', [])
10
10
11
11
. constant ( 'uiDateConfig' , { } )
12
12
13
- . directive ( 'uiDate' , [ 'uiDateConfig' , function ( uiDateConfig ) {
13
+ . directive ( 'uiDate' , [ 'uiDateConfig' , 'uiDateConverter' , function ( uiDateConfig , uiDateConverter ) {
14
14
'use strict' ;
15
15
var options ;
16
16
options = { } ;
@@ -58,7 +58,11 @@ angular.module('ui.date', [])
58
58
controller . $render = function ( ) {
59
59
var date = controller . $modelValue ;
60
60
if ( angular . isDefined ( date ) && date !== null && ! angular . isDate ( date ) ) {
61
- throw new Error ( 'ng-Model value must be a Date object - currently it is a ' + typeof date + ' - use ui-date-format to convert it from a string' ) ;
61
+ if ( angular . isString ( controller . $modelValue ) ) {
62
+ date = uiDateConverter . stringToDate ( attrs . uiDateFormat , controller . $modelValue )
63
+ } else {
64
+ throw new Error ( 'ng-Model value must be a Date, or a String object with a date formatter - currently it is a ' + typeof date + ' - use ui-date-format to convert it from a string' ) ;
65
+ }
62
66
}
63
67
element . datepicker ( "setDate" , date ) ;
64
68
} ;
@@ -78,45 +82,54 @@ angular.module('ui.date', [])
78
82
} ;
79
83
}
80
84
] )
85
+ . service ( 'uiDateConverter' , [ 'uiDateFormatConfig' , function ( uiDateFormatConfig ) {
81
86
82
- . constant ( 'uiDateFormatConfig' , '' )
87
+ return {
88
+ stringToDate : stringToDate ,
89
+ dateToString : dateToString
90
+ } ;
91
+
92
+ function dateToString ( dateFormat , value ) {
93
+ if ( value ) {
94
+ if ( dateFormat || uiDateFormatConfig ) {
95
+ return jQuery . datepicker . formatDate ( dateFormat , value ) ;
96
+ }
97
+ return value . toISOString ( ) ;
98
+ } else {
99
+ return null ;
100
+ }
101
+ }
83
102
84
- . directive ( 'uiDateFormat' , [ 'uiDateFormatConfig' , function ( uiDateFormatConfig ) {
103
+ function stringToDate ( dateFormat , value ) {
104
+ if ( angular . isString ( value ) ) {
105
+ if ( dateFormat || uiDateFormatConfig ) {
106
+ return jQuery . datepicker . parseDate ( dateFormat , value ) ;
107
+ }
108
+
109
+ var isoDate = new Date ( value ) ;
110
+ return isNaN ( isoDate . getTime ( ) ) ? null : isoDate ;
111
+ }
112
+ return null ;
113
+ }
114
+ } ] )
115
+ . constant ( 'uiDateFormatConfig' , '' )
116
+ . directive ( 'uiDateFormat' , [ 'uiDateConverter' , function ( uiDateConverter ) {
85
117
var directive = {
86
118
require :'ngModel' ,
87
119
link : function ( scope , element , attrs , modelCtrl ) {
88
- var dateFormat = attrs . uiDateFormat || uiDateFormatConfig ;
89
- if ( dateFormat ) {
120
+ var dateFormat = attrs . uiDateFormat ;
121
+
90
122
// Use the datepicker with the attribute value as the dateFormat string to convert to and from a string
91
123
modelCtrl . $formatters . unshift ( function ( value ) {
92
- if ( angular . isString ( value ) ) {
93
- return jQuery . datepicker . parseDate ( dateFormat , value ) ;
94
- }
95
- return null ;
96
- } ) ;
97
- modelCtrl . $parsers . push ( function ( value ) {
98
- if ( value ) {
99
- return jQuery . datepicker . formatDate ( dateFormat , value ) ;
100
- }
101
- return null ;
102
- } ) ;
103
- } else {
104
- // Default to ISO formatting
105
- modelCtrl . $formatters . unshift ( function ( value ) {
106
- if ( angular . isString ( value ) ) {
107
- var isoDate = new Date ( value ) ;
108
- return isNaN ( isoDate . getTime ( ) ) ? null : isoDate ;
109
- }
110
- return null ;
124
+ return uiDateConverter . stringToDate ( dateFormat , value ) ;
111
125
} ) ;
126
+
112
127
modelCtrl . $parsers . push ( function ( value ) {
113
- if ( value ) {
114
- return value . toISOString ( ) ;
115
- }
116
- return null ;
128
+ return uiDateConverter . dateToString ( dateFormat , value )
117
129
} ) ;
118
- }
130
+
119
131
}
120
132
} ;
133
+
121
134
return directive ;
122
135
} ] ) ;
0 commit comments