1- // pick-a-date (attribute)
21angular . module ( 'ng' ) . directive ( 'pickADate' , function ( ) {
32 return {
43 restrict : "A" ,
4+ require : 'ngModel' ,
55 scope : {
6- pickADate : '=' ,
6+ ngModel : '=' ,
77 minDate : '=' ,
88 maxDate : '='
99 } ,
10- link : function ( scope , element , attrs ) {
10+ link : function ( scope , element , attrs , ngModel ) {
1111 element . pickadate ( {
12- onSet : function ( e ) {
13- if ( scope . $$phase || scope . $root . $$phase ) // we are coming from $watch or link setup
14- return ;
15- var select = element . pickadate ( 'picker' ) . get ( 'select' ) ; // selected date
16- scope . $apply ( function ( ) {
17- if ( e . hasOwnProperty ( 'clear' ) ) {
18- scope . pickADate = null ;
19- return ;
20- }
21- if ( ! scope . pickADate )
22- scope . pickADate = new Date ( 0 ) ;
23- scope . pickADate . setYear ( select . obj . getFullYear ( ) ) ;
24- // Interesting: getYear returns only since 1900. Use getFullYear instead.
25- // It took me half a day to figure that our. Ironically setYear()
26- // (not setFullYear, duh) accepts the actual year A.D.
27- // So as I got the $#%^ 114 and set it, guess what, I was transported to ancient Rome 114 A.D.
28- // That's it I'm done being a programmer, I'd rather go serve Emperor Trajan as a sex slave.
29- scope . pickADate . setMonth ( select . obj . getMonth ( ) ) ;
30- scope . pickADate . setDate ( select . obj . getDate ( ) ) ;
31- } ) ;
32- } ,
3312 onClose : function ( ) {
3413 element . blur ( ) ;
3514 }
3615 } ) ;
3716 function updateValue ( newValue ) {
3817 if ( newValue ) {
39- scope . pickADate = ( newValue instanceof Date ) ? newValue : new Date ( newValue ) ;
40- // needs to be in milliseconds
4118 element . pickadate ( 'picker' ) . set ( 'select' , scope . pickADate . getTime ( ) ) ;
4219 } else {
4320 element . pickadate ( 'picker' ) . clear ( ) ;
44- scope . pickADate = null ;
4521 }
4622 }
4723 updateValue ( scope . pickADate ) ;
4824 element . pickadate ( 'picker' ) . set ( 'min' , scope . minDate ? scope . minDate : false ) ;
4925 element . pickadate ( 'picker' ) . set ( 'max' , scope . maxDate ? scope . maxDate : false ) ;
50- scope . $watch ( 'pickADate' , function ( newValue , oldValue ) {
26+
27+ scope . $watch ( scope . ngModel , function ( newValue , oldValue ) {
5128 if ( newValue == oldValue )
5229 return ;
5330 updateValue ( newValue ) ;
@@ -61,18 +38,3 @@ angular.module('ng').directive('pickADate', function () {
6138 }
6239 } ;
6340} ) ;
64-
65- //--------- other misc shit ---------------
66-
67- function testController ( $scope ) {
68- $scope . curDate = '' ;
69- $scope . newDate = function ( ) {
70- return new Date ( ) ;
71- } ;
72- }
73-
74- function testController2 ( $scope ) {
75- $scope . startDate = '2014-02-24 12:00:00' ;
76- $scope . endDate = '2014-02-27 12:00:00' ;
77- }
78-
0 commit comments