Skip to content

Commit

Permalink
feat(date): Watch for model date value changes and not just model cha…
Browse files Browse the repository at this point in the history
…nges

closes angular-ui#82
  • Loading branch information
alexanderchan committed Dec 3, 2015
1 parent b357626 commit 496bdd6
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
19 changes: 16 additions & 3 deletions demo/demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,36 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>AngularUI - Date Picker Demo</title>
<base href=".."></base>

<!-- Styles -->
<link rel="stylesheet" href="bower_components/jquery-ui/themes/smoothness/jquery-ui.css">
</head>

<body ng-app="ui.date">
<body ng-app="Demo">
<div ng-controller="DemoCtrl">
<!-- Date -->
<label for="date">Select Date:</label>
<input type="text" id="date" ui-date ng-model="aDate">

<div>{{ aDate }}</div>

<div><a href="" ng-click="incDate()">Increase Date</a></div>

</div>
<!-- Scripts -->
<script type="text/javascript" src="bower_components/jquery/dist/jquery.js"></script>
<script type="text/javascript" src="bower_components/jquery-ui/jquery-ui.min.js"></script>
<script type="text/javascript" src="bower_components/angular/angular.js"></script>
<script type="text/javascript" src="src/date.js"></script>
<script>
angular.module('Demo', ['ui.date'])
.controller('DemoCtrl', ['$scope', function($scope){
$scope.aDate = new Date();
$scope.incDate = function() {
$scope.aDate.setDate($scope.aDate.getDate()+1);
}
}])
</script>
</body>

</html>
14 changes: 14 additions & 0 deletions src/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,20 @@ if (typeof module !== 'undefined' && typeof exports !== 'undefined' && module.ex
}
};

// Watch for changes to values in the date (and not just reference changes)
scope.$watch(function() {
if (controller && angular.isDate(controller.$modelValue)) {
return controller.$modelValue.getDate();
} else {
return null;
}
},
function() {
if (controller) {
controller.$render();
}
});

// Watch for changes to the directives options
scope.$watch(getOptions, initDateWidget, true);
}
Expand Down
15 changes: 15 additions & 0 deletions test/date.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,21 @@ describe('uiDate', function() {
expect(element.datepicker('getDate')).toEqual(aDate);
});
});

it('should update the datepicker if the model changes', function() {
inject(function($compile, $rootScope) {
var aDate, element;
aDate = new Date(2010, 12, 1);
element = $compile('<input ui-date ng-model="x"/>')($rootScope);
$rootScope.$apply(function() {
$rootScope.x = aDate;
});
aDate.setDate(aDate.getDate() + 1);
$rootScope.$apply();
expect(element.datepicker('getDate')).toEqual(aDate);
});
});

it('should put the date in the model', function() {
inject(function($compile, $rootScope) {
var aDate, element;
Expand Down

0 comments on commit 496bdd6

Please sign in to comment.