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

Commit

Permalink
fix(datepicker): fire ngChange on today/clear button press
Browse files Browse the repository at this point in the history
Closes #1379
  • Loading branch information
bekos committed Dec 10, 2013
1 parent 93cd0df commit 6b1c68f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/datepicker/datepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,10 @@ function ($compile, $parse, $document, $position, dateFilter, datepickerPopupCon
ngModel.$parsers.unshift(parseDate);

// Inner change
scope.dateSelection = function() {
scope.dateSelection = function(dt) {
if (angular.isDefined(dt)) {
scope.date = dt;
}
ngModel.$setViewValue(scope.date);
ngModel.$render();

Expand Down Expand Up @@ -442,13 +445,11 @@ function ($compile, $parse, $document, $position, dateFilter, datepickerPopupCon
}
});

var $setModelValue = $parse(attrs.ngModel).assign;

scope.today = function() {
$setModelValue(originalScope, new Date());
scope.dateSelection(new Date());
};
scope.clear = function() {
$setModelValue(originalScope, null);
scope.dateSelection(null);
};

var $popup = $compile(popupEl)(scope);
Expand Down
30 changes: 30 additions & 0 deletions src/datepicker/test/datepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,36 @@ describe('datepicker directive', function () {
expect(buttonBarElement.css('display')).toBe('none');
});
});

describe('`ng-change`', function() {
beforeEach(inject(function() {
$rootScope.changeHandler = jasmine.createSpy('changeHandler');
var wrapElement = $compile('<div><input ng-model="date" datepicker-popup ng-change="changeHandler()"><div>')($rootScope);
$rootScope.$digest();
assignElements(wrapElement);
assignButtonBar();
}));

it('should be called when `today` is clicked', function() {
buttons.eq(0).click();
expect($rootScope.changeHandler).toHaveBeenCalled();
});

it('should not be called when `weeks` is clicked', function() {
buttons.eq(1).click();
expect($rootScope.changeHandler).not.toHaveBeenCalled();
});

it('should be called when `clear` is clicked', function() {
buttons.eq(2).click();
expect($rootScope.changeHandler).toHaveBeenCalled();
});

it('should not be called when `close` is clicked', function() {
buttons.eq(3).click();
expect($rootScope.changeHandler).not.toHaveBeenCalled();
});
});
});

describe('use with `ng-required` directive', function() {
Expand Down

0 comments on commit 6b1c68f

Please sign in to comment.