Created
February 17, 2014 22:54
-
-
Save mackstar/9060893 to your computer and use it in GitHub Desktop.
User Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
app.config(['$routeProvider', function($routeProvider) { | |
$routeProvider | |
.when('/add', { | |
templateUrl: '/js/templates/users/edit.html', | |
controller: 'UserAddCtrl' | |
}); | |
}]); | |
app.controller('UserAddCtrl', function($scope, $rootScope, Restangular, parseFormErrors, $location) { | |
var ready = false; | |
$rootScope.$emit('modal.open', true); | |
$scope.$on('roles.loaded', function() { | |
ready = true; | |
}); | |
$scope.ready = function() { | |
return ready; | |
} | |
$scope.close = function() { | |
$rootScope.$emit('modal.close', true); | |
$location.path('/users'); | |
} | |
$scope.user = { | |
email: '', | |
password: '', | |
name: '' | |
}; | |
$scope.submit = function() { | |
/* If form is invalid */ | |
if ($scope.userForm.$invalid) { | |
$rootScope.$emit('sp.message', {title: 'Oops', message: 'The form is not yet complete', type: "danger"}); | |
return; | |
} | |
/* Make post request */ | |
Restangular.all('users/index').post($scope.user).then(function() { | |
$rootScope.$emit('sp.message', {title: 'Yeah!', message: 'User saved successfully', type: "success"}); | |
$rootScope.$emit('users.reload', true); | |
$rootScope.$emit('modal.close', true); | |
$location.path('/users'); | |
}, function(response) { | |
parseFormErrors(response.data, $scope.userForm); | |
}); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment