Skip to content
This repository was archived by the owner on May 29, 2019. It is now read-only.

Commit 0ecf7fa

Browse files
decafdenniswesleycho
authored andcommitted
fix(datepicker): Parse date from $viewValue instead of $modelValue
Allow use in conjunction with an ngModelController that has custom formatters and parsers to translate between $modelValue and $viewValue
1 parent f9a9b97 commit 0ecf7fa

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

src/datepicker/datepicker.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
6363
};
6464

6565
this.render = function() {
66-
if ( ngModelCtrl.$modelValue ) {
67-
var date = new Date( ngModelCtrl.$modelValue ),
66+
if ( ngModelCtrl.$viewValue ) {
67+
var date = new Date( ngModelCtrl.$viewValue ),
6868
isValid = !isNaN(date);
6969

7070
if ( isValid ) {
@@ -81,13 +81,13 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
8181
if ( this.element ) {
8282
this._refreshView();
8383

84-
var date = ngModelCtrl.$modelValue ? new Date(ngModelCtrl.$modelValue) : null;
84+
var date = ngModelCtrl.$viewValue ? new Date(ngModelCtrl.$viewValue) : null;
8585
ngModelCtrl.$setValidity('date-disabled', !date || (this.element && !this.isDisabled(date)));
8686
}
8787
};
8888

8989
this.createDateObject = function(date, format) {
90-
var model = ngModelCtrl.$modelValue ? new Date(ngModelCtrl.$modelValue) : null;
90+
var model = ngModelCtrl.$viewValue ? new Date(ngModelCtrl.$viewValue) : null;
9191
return {
9292
date: date,
9393
label: dateFilter(date, format),
@@ -112,7 +112,7 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
112112

113113
$scope.select = function( date ) {
114114
if ( $scope.datepickerMode === self.minMode ) {
115-
var dt = ngModelCtrl.$modelValue ? new Date( ngModelCtrl.$modelValue ) : new Date(0, 0, 0, 0, 0, 0, 0);
115+
var dt = ngModelCtrl.$viewValue ? new Date( ngModelCtrl.$viewValue ) : new Date(0, 0, 0, 0, 0, 0, 0);
116116
dt.setFullYear( date.getFullYear(), date.getMonth(), date.getDate() );
117117
ngModelCtrl.$setViewValue( dt );
118118
ngModelCtrl.$render();

src/datepicker/test/datepicker.spec.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,26 @@ describe('datepicker directive', function () {
66
beforeEach(module('template/datepicker/month.html'));
77
beforeEach(module('template/datepicker/year.html'));
88
beforeEach(module('template/datepicker/popup.html'));
9+
beforeEach(module(function($compileProvider) {
10+
$compileProvider.directive('dateModel', function() {
11+
return {
12+
restrict: 'A',
13+
require: 'ngModel',
14+
link: function(scope, element, attrs, modelController) {
15+
modelController.$formatters.push(function(object) {
16+
return new Date(object.date);
17+
});
18+
19+
modelController.$parsers.push(function(date) {
20+
return {
21+
type: 'date',
22+
date: date.toUTCString()
23+
};
24+
});
25+
}
26+
};
27+
});
28+
}));
929
beforeEach(inject(function(_$compile_, _$rootScope_) {
1030
$compile = _$compile_;
1131
$rootScope = _$rootScope_;
@@ -1732,4 +1752,30 @@ describe('datepicker directive', function () {
17321752
expect(getTitle()).toBe('2013');
17331753
});
17341754
});
1755+
1756+
describe('with an ngModelController having formatters and parsers', function() {
1757+
beforeEach(inject(function() {
1758+
// Custom date object.
1759+
$rootScope.date = { type: 'date', date: 'April 1, 2015 00:00:00' };
1760+
1761+
// Use dateModel directive to add formatters and parsers to the
1762+
// ngModelController that translate the custom date object.
1763+
element = $compile('<datepicker ng-model="date" date-model></datepicker>')($rootScope);
1764+
$rootScope.$digest();
1765+
}));
1766+
1767+
it('updates the view', function() {
1768+
$rootScope.date = { type: 'date', date: 'April 15, 2015 00:00:00' };
1769+
$rootScope.$digest();
1770+
1771+
expectSelectedElement(17);
1772+
});
1773+
1774+
it('updates the model', function() {
1775+
clickOption(17);
1776+
1777+
expect($rootScope.date.type).toEqual('date');
1778+
expect(new Date($rootScope.date.date)).toEqual(new Date('April 15, 2015 00:00:00'));
1779+
});
1780+
});
17351781
});

0 commit comments

Comments
 (0)