angular.module('EssayApp.directives').directive "spinner", ['$timeout', ($timeout) -> restrict: 'A' scope: true require: "?ngModel" link: (scope, element, attrs, ngModel) -> defaults = min: 0 max: 10000 step: 1 spin: (event, ui) -> ngModel.$setViewValue(ui.value) scope.$apply() change: (event, ui) -> elem = $(this) max = elem.spinner("option", "max") if elem.spinner("value") > max elem.val(max) elem.trigger("change") return true # Take a hash of options from the chosen directive options = scope.$eval(attrs.spinner) or {} # Merge options with default options options = $.extend defaults, options $timeout (-> element.spinner(options) return ), 0, false ]