Skip to content

Instantly share code, notes, and snippets.

@mackstar
Created February 17, 2014 22:54
Show Gist options
  • Save mackstar/9060893 to your computer and use it in GitHub Desktop.
Save mackstar/9060893 to your computer and use it in GitHub Desktop.
User Controller
'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