Skip to content

Commit

Permalink
Added an 'on-change' attribute
Browse files Browse the repository at this point in the history
Calls the passed in functin when the model changes
  • Loading branch information
adamalbrecht committed Nov 26, 2013
1 parent 884fa07 commit e24418c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ There are a number of options that be configured inline with attributes. Here ar
| placeholder | 'Click to Set Date' | Text that is shown on button when the model variable is null. |
| hover-text | null | Hover text for button. |
| icon-class | null | If set, `<i class='some-class'></i>` will be prepended inside the button |
| disable-timepicker | false | If true, the timepicker will be disabled and the default label format will be just the date |
| disable-timepicker | false | If true, the timepicker will be disabled and the default label format will be just the date |
| disable-clear-button | false | If true, the clear button will be removed |
| on-change | null | Set to a function that will be called when the date is changed |

**Example:**

Expand Down
40 changes: 40 additions & 0 deletions spec/basic-specs.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -210,5 +210,45 @@ describe "ngQuickDate", ->
expect(element.scope().ngModel).toEqual(null)


describe "Given a datepicker with an 'on-change' method to call", ->
mySpy = undefined
beforeEach(inject(($compile, $rootScope) ->
scope = $rootScope
scope.myVariable = 1
scope.myOtherVariable = null
scope.myMethod = (param) ->
scope.myVariable += 1
scope.myOtherVariable = param

scope.myDate = new Date(2013, 5, 10)
element = $compile("<datepicker ng-model='myDate' on-change='myMethod(\"hello!\")' />")(scope)
scope.$apply()
))

it 'should not be called initially', ->
expect(scope.myVariable).toEqual(1)

# Can't get this spec to work
describe 'When the date input is changed', ->
beforeEach ->
$input = $(element).find('.quickdate-date-input')
scope.$apply ->
$input.val('1/5/2013')
$input.trigger('input')
$input.trigger('blur')
browserTrigger($input, 'input')
browserTrigger($input, 'change')
browserTrigger($input, 'blur')

it 'should call the method once', ->
expect(scope.myVariable).toEqual(2)
expect(scope.myOtherVariable).toEqual('hello!')

describe 'When the date input is blurred but not changed', ->
beforeEach ->
$input = $(element).find('.quickdate-date-input')
browserTrigger($input, 'change')
browserTrigger($input, 'blur')

it 'should not call the method', ->
expect(scope.myVariable).toEqual(1)
16 changes: 11 additions & 5 deletions src/ng-quick-date.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ app.directive "datepicker", ['ngQuickDateDefaults', '$filter', (ngQuickDateDefau
require: "ngModel"
scope:
ngModel: "="
onChange: "&"

replace: true
link: (scope, element, attrs, ngModel) ->
Expand Down Expand Up @@ -179,27 +180,32 @@ app.directive "datepicker", ['ngQuickDateDefaults', '$filter', (ngQuickDateDefau
scope.calendarShown = not scope.calendarShown

scope.setDate = (date, closeCalendar=true) ->
changed = (!scope.ngModel && date) || (scope.ngModel && !date) || (date.getTime() != scope.ngModel.getTime())
scope.ngModel = date
scope.toggleCalendar(false)
if changed && scope.onChange
scope.onChange()

scope.setDateFromInput = (closeCalendar=false) ->
try
tmpDate = parseDateString(scope.inputDate)
if !tmpDate
throw 'Invalid Date'
if scope.inputTime and scope.inputTime.length and tmpDate
if !scope.disableTimepicker && scope.inputTime and scope.inputTime.length and tmpDate
tmpTime = if scope.disableTimepicker then '00:00:00' else scope.inputTime
tmpDateAndTime = parseDateString("#{scope.inputDate} #{tmpTime}")
if !tmpDateAndTime
throw 'Invalid Time'
scope.ngModel = tmpDateAndTime
scope.setDate(tmpDateAndTime, false)
else
scope.ngModel = tmpDate
scope.setDate(tmpDate, false)

if closeCalendar
scope.toggleCalendar(false)

scope.inputDateErr = false
scope.inputTimeErr = false

catch err
if err == 'Invalid Date'
scope.inputDateErr = true
Expand Down Expand Up @@ -241,11 +247,11 @@ app.directive "datepicker", ['ngQuickDateDefaults', '$filter', (ngQuickDateDefau
<div class='quickdate-text-inputs'>
<div class='quickdate-input-wrapper'>
<label>Date</label>
<input class='quickdate-date-input' name='inputDate' type='text' ng-model='inputDate' placeholder='1/1/2013' ng-blur='setDateFromInput()' ng-enter='setDateFromInput(true)' ng-class="{'ng-quick-date-error': inputDateErr}" ng-tab='onDateInputTab()' />
<input class='quickdate-date-input' name='inputDate' type='text' ng-model='inputDate' placeholder='1/1/2013' ng-blur="setDateFromInput()" ng-enter="setDateFromInput(true)" ng-class="{'ng-quick-date-error': inputDateErr}" ng-tab='onDateInputTab()' />
</div>
<div class='quickdate-input-wrapper' ng-hide='disableTimepicker'>
<label>Time</label>
<input class='quickdate-time-input' name='inputTime' type='text' ng-model='inputTime' placeholder='12:00 PM' ng-blur='setDateFromInput()' ng-enter='setDateFromInput(true)' ng-class="{'quickdate-error': inputTimeErr}" ng-tab='onTimeInputTab()'>
<input class='quickdate-time-input' name='inputTime' type='text' ng-model='inputTime' placeholder='12:00 PM' ng-blur="setDateFromInput(false)" ng-enter="setDateFromInput(true)" ng-class="{'quickdate-error': inputTimeErr}" ng-tab='onTimeInputTab()'>
</div>
</div>
<div class='quickdate-calendar-header'>
Expand Down

0 comments on commit e24418c

Please sign in to comment.