Skip to content

Instantly share code, notes, and snippets.

@sajal
Forked from dandoyon/date.coffee.js
Created August 13, 2012 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sajal/3342925 to your computer and use it in GitHub Desktop.
Save sajal/3342925 to your computer and use it in GitHub Desktop.
Coffeescript generated javascript for angular-ui ui-date directive
/*
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', function(uiConfig) {
var options;
options = {};
if (uiConfig.date != null) {
angular.extend(options, uiConfig.date);
}
return {
require: '?ngModel',
link: function(scope, element, attrs, controller) {
var opts, updateModel, usersOnSelectHandler;
opts = angular.extend({}, options, scope.$eval(attrs.uiDate));
/* If we have a controller (i.e. ngModelController) then wire it up
*/
if (controller != null) {
updateModel = function(value, picker) {
return scope.$apply(function() {
return controller.$setViewValue(element.datepicker("getDate"));
});
};
if (opts.onSelect != null) {
/* Caller has specified onSelect to call this as well as updating the model
*/
usersOnSelectHandler = opts.onSelect;
opts.onSelect = function(value, picker) {
updateModel(value);
return usersOnSelectHandler(value, picker);
};
} else {
/* No onSelect already specified so just update the model
*/
opts.onSelect = updateModel;
}
/* Update the date picker when the model changes
*/
controller.$render = function() {
var date;
date = controller.$viewValue;
if (!(date instanceof Date)) {
date = new Date(date);
}
return element.datepicker("setDate", date);
};
}
/* Watch uiDate attr and set datepicker options
*/
scope.$watch(attrs.uiDate, function(options){
Object.keys(options).forEach(function(o){
element.datepicker( "option", o, options[o] )
})
}, true);
/* Create the datepicker widget
*/
return element.datepicker(opts);
}
};
}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment