Permalink
Browse files

Merge branch 'watch-date-options' of git://github.com/petebacondarwin…

…/angular-ui

* 'watch-date-options' of git://github.com/petebacondarwin/angular-ui:
  Iterating over the new options
  Some work on date directive

Conflicts:
	modules/directives/date/date.coffee
  • Loading branch information...
Dean Sofer
Dean Sofer committed Aug 20, 2012
2 parents aaf11a5 + ac440f6 commit 09778fdfc97bcdba4b372394ba3e38915e42e9bd
@@ -1,42 +1,50 @@
+
###
jQuery UI Datepicker plugin wrapper
@param [ui-date] {object} Options to pass to $.fn.datepicker() merged onto ui.config
###
angular.module('ui.directives').directive 'uiDate', ['ui.config', (uiConfig)->
- options = {}
- ;
+ options = {};
if uiConfig.date?
angular.extend(options, uiConfig.date)
-
require: '?ngModel',
link: (scope, element, attrs, controller)->
- opts = angular.extend({}, options, scope.$eval(attrs.uiDate))
- ;
-
- ### If we have a controller (i.e. ngModelController) then wire it up ###
- if controller?
- updateModel = (value, picker)->
- scope.$apply ()->
- controller.$setViewValue(element.datepicker("getDate"))
-
- if opts.onSelect?
- ### Caller has specified onSelect to call this as well as updating the model ###
- usersOnSelectHandler = opts.onSelect
- opts.onSelect = (value, picker)->
- updateModel(value)
- usersOnSelectHandler(value, picker)
- else
- ### No onSelect already specified so just update the model ###
- opts.onSelect = updateModel
+ initDateWidget = ()->
+ opts = angular.extend({}, options, scope.$eval(attrs.uiDate))
+ console.log 'value:', scope.$eval(attrs.uiDate)
+ console.log 'opts:', opts
+
+ ### If we have a controller (i.e. ngModelController) then wire it up ###
+ if controller?
+ updateModel = (value, picker)->
+ scope.$apply ()->
+ controller.$setViewValue(element.datepicker("getDate"))
- ### Update the date picker when the model changes ###
- controller.$render = ()->
- date = controller.$viewValue
- ### We don't need to convert to a Date here since setDate accepts a string ###
- element.datepicker("setDate", date)
+ if opts.onSelect?
+ ### Caller has specified onSelect to call this as well as updating the model ###
+ usersOnSelectHandler = opts.onSelect
+ opts.onSelect = (value, picker)->
+ updateModel(value)
+ usersOnSelectHandler(value, picker)
+ else
+ ### No onSelect already specified so just update the model ###
+ opts.onSelect = updateModel
- ### Create the datepicker widget ###
- element.datepicker(opts)
+ ### Update the date picker when the model changes ###
+ controller.$render = ()->
+ date = controller.$viewValue
+ date = new Date(date) unless date instanceof Date
+ element.datepicker("setDate", date)
-]
+ ### Create the datepicker widget ###
+ element.datepicker(opts)
+ console.log 'minDate', element.datepicker( "option", "minDate" )
+
+ updateDateOptions = (options)->
+ for own name, value of options
+ element.datepicker('option', name, value)
+
+ scope.$watch(attrs.uiDate, updateDateOptions, true)
+ initDateWidget()
+]
@@ -113,4 +113,15 @@ describe 'uiDate', ()->
aDate = new Date(2010, 12, 1)
element = $compile("<div ui-date ng-model='x' ng-required='true' ></div>")($rootScope)
selectDate(element, aDate)
- expect(element.hasClass('ng-valid')).toBeTruthy()
+ expect(element.hasClass('ng-valid')).toBeTruthy()
+
+ describe 'when attribute options change', ()->
+ it 'should watch attribute and update date widget accordingly', ()->
+ inject ($compile, $rootScope)->
+ $rootScope.config = minDate: 5
+ element = $compile("<input ui-date='config' />")($rootScope)
+ $rootScope.$apply()
+ expect(element.datepicker( "option", "minDate" )).toBe(5);
+ $rootScope.$apply ()->
+ $rootScope.config.minDate = 10
+ expect(element.datepicker( "option", "minDate" )).toBe(10);

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.

0 comments on commit 09778fd

Please sign in to comment.