Skip to content

Instantly share code, notes, and snippets.

@dougluce
Last active December 16, 2015 15:19
Show Gist options
  • Save dougluce/5455020 to your computer and use it in GitHub Desktop.
Save dougluce/5455020 to your computer and use it in GitHub Desktop.
Litcoffee not doing the right thing? Or is that me?
// Generated by CoffeeScript 1.6.2
(function() {
$scope.goto = function(url) {
return $window.location.href = '/';
};
$scope.delay = function(test) {
if ($scope.form_submitted) {
return test;
}
};
$scope.needsFixed = function() {
return $scope.form_submitted && $scope.form.$invalid;
};
$scope.errorUnlessValid = function(name) {
return {
error: $scope.delay($scope.form[name].$invalid)
};
};
$scope.resetValidity = function(type) {
var field, status, _ref;
_ref = type.split('/'), field = _ref[0], status = _ref[1];
return $scope.form[field].$setValidity(status, true);
};
$scope.update = function(user) {
$scope.form_submitted = true;
if ($scope.needsFixed()) {
return;
}
$scope.form_disabled = true;
return Signup.save(user, function() {
return $('#success').foundation('reveal', 'open');
}, function(response) {
$scope.form_disabled = false;
if (response.data === 'username-taken') {
return $scope.form.user.$setValidity('taken', false);
} else if (response.data === 'email-taken') {
return $scope.form.email.$setValidity('taken', false);
}
});
};
}).call(this);

Dude!

app = angular.module 'signup', ['ngResource']

app.factory 'Signup', ($resource) -> $resource '/signup'

errormsg(type="fieldname/statustype")

where fieldname is the form field we are watching

and statustype is the type of error we add an error class for.

This is wrapped in the scope delay() call.

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))

"value taken" validator -- all this really does is reset the

validity of the field upon input. This is used

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() {

redirect to homepage on new account creation, add short delay so user can read alert window //

$('.modal-alert #ok').click(function(){ setTimeout(function(){window.location.href = '/';}, 300)});

#}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment