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

Commit

Permalink
fix(timepicker): move render logic to formatter
Browse files Browse the repository at this point in the history
- Moves render logic converting model values into date or null object into a formatter, which allows the render function to more correctly render using the viewValue

- remove parentheses as per CR

Relates to #2069

Fixes #3160
Closes #3427
  • Loading branch information
wesleycho authored and chrisirhc committed Apr 5, 2015
1 parent 33269bb commit b4bbc01
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/timepicker/test/timepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ describe('timepicker directive', function () {
expect(getModelState()).toEqual([14, 40]);
});

it('should be pristine', function() {
expect(element.controller('ngModel').$pristine).toBe(true);
});

it('has `selected` current time when model is initially cleared', function() {
$rootScope.time = null;
element = $compile('<timepicker ng-model="time"></timepicker>')($rootScope);
Expand Down Expand Up @@ -773,6 +777,36 @@ describe('timepicker directive', function () {
});
});

describe('$formatter', function () {
var ngModel,
date;

beforeEach(function () {
ngModel = element.controller('ngModel');
date = new Date('Mon Mar 23 2015 14:40:11 GMT-0700 (PDT)');
});

it('should have one formatter', function () {
expect(ngModel.$formatters.length).toBe(1);
});

it('should convert a date to a new reference representing the same date', function () {
expect(ngModel.$formatters[0](date)).toEqual(date);
});

it('should convert a valid date string to a date object', function () {
expect(ngModel.$formatters[0]('Mon Mar 23 2015 14:40:11 GMT-0700 (PDT)')).toEqual(date);
});

it('should set falsy values as null', function () {
expect(ngModel.$formatters[0](undefined)).toBe(null);
expect(ngModel.$formatters[0](null)).toBe(null);
expect(ngModel.$formatters[0]('')).toBe(null);
expect(ngModel.$formatters[0](0)).toBe(null);
expect(ngModel.$formatters[0](NaN)).toBe(null);
});
});

describe('user input validation', function () {
var changeInputValueTo;

Expand Down
6 changes: 5 additions & 1 deletion src/timepicker/timepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ angular.module('ui.bootstrap.timepicker', [])
ngModelCtrl = ngModelCtrl_;
ngModelCtrl.$render = this.render;

ngModelCtrl.$formatters.unshift(function (modelValue) {
return modelValue ? new Date( modelValue ) : null;
});

var hoursInputEl = inputs.eq(0),
minutesInputEl = inputs.eq(1);

Expand Down Expand Up @@ -208,7 +212,7 @@ angular.module('ui.bootstrap.timepicker', [])
};

this.render = function() {
var date = ngModelCtrl.$modelValue ? new Date( ngModelCtrl.$modelValue ) : null;
var date = ngModelCtrl.$viewValue;

if ( isNaN(date) ) {
ngModelCtrl.$setValidity('time', false);
Expand Down

0 comments on commit b4bbc01

Please sign in to comment.