app = angular.module 'signup', ['ngResource']
app.factory 'Signup', ($resource) -> $resource '/signup'
app.directive 'errormsg', ($compile) -> restrict: 'E' link: (scope, elm, attrs) -> [field, status] = attrs.type.split('/') templ = angular.element('') .attr('ng-show', ['delay(form.', field, '.$error.', status, ')'].join('')) .html elm.html() templ.addClass cls for cls in ['error', 'ng-cloak'] elm.replaceWith($compile(templ)(scope))
app.directive 'taken', -> require: 'ngModel' link: (scope, elm, attrs, ctrl) -> ctrl.$parsers.unshift (viewValue) -> ctrl.$setValidity 'taken', true viewValue
app.controller 'SignupController', ($scope, $window, Signup) -> $scope.form_submitted = true # false $scope.checkPassword = -> $scope.form.password.$setValidity 'dontMatch', $scope.user.password == $scope.user.password2
$scope.goto = (url) ->
$window.location.href = '/'
$scope.delay = (test) ->
test if $scope.form_submitted
$scope.needsFixed = ->
$scope.form_submitted and $scope.form.$invalid
$scope.errorUnlessValid = (name) ->
{error: $scope.delay($scope.form[name].$invalid)}
$scope.resetValidity = (type) ->
[field, status] = type.split('/')
$scope.form[field].$setValidity(status,true)
$scope.update = (user) ->
$scope.form_submitted = true
return if $scope.needsFixed()
$scope.form_disabled=true
Signup.save user, -> # Success
$('#success').foundation 'reveal', 'open'
, (response) -> # Failure
$scope.form_disabled=false
if response.data == 'username-taken'
$scope.form.user.$setValidity 'taken', false
else if response.data == 'email-taken'
$scope.form.email.$setValidity 'taken', false
#function SignupController() {
#}