Skip to content
Snippets Groups Projects
Commit 7c131fd4 authored by Dapeng's avatar Dapeng
Browse files

add simple error handler for OwnerController.js

parent 080f9e41
No related branches found
No related tags found
No related merge requests found
...@@ -12,8 +12,8 @@ function loadOwner($scope, $resource, $stateParams) { ...@@ -12,8 +12,8 @@ function loadOwner($scope, $resource, $stateParams) {
*/ */
angular.module('controllers').controller('ownerSearchController', ['$scope', '$state', angular.module('controllers').controller('ownerSearchController', ['$scope', '$state',
function($scope, $state) { function($scope, $state) {
$scope.ownerSearchForm = {}; $scope.ownerSearchForm = {};
// form always needs to be initialised // form always needs to be initialised
// otherwise we can't read $scope.ownerSearchForm.lastName // otherwise we can't read $scope.ownerSearchForm.lastName
...@@ -27,13 +27,13 @@ angular.module('controllers').controller('ownerSearchController', ['$scope', '$s ...@@ -27,13 +27,13 @@ angular.module('controllers').controller('ownerSearchController', ['$scope', '$s
*/ */
angular.module('controllers').controller('ownerListController', ['$scope', '$resource', '$stateParams', angular.module('controllers').controller('ownerListController', ['$scope', '$resource', '$stateParams',
function($scope, $resource, $stateParams) { function($scope, $resource, $stateParams) {
var destUrl = '/petclinic/owner/list?lastName='; var destUrl = '/petclinic/owner/list?lastName=';
if(angular.isDefined($stateParams.lastName)) { if(angular.isDefined($stateParams.lastName)) {
destUrl += $stateParams.lastName; destUrl += $stateParams.lastName;
} }
var ownerResource = $resource(destUrl); var ownerResource = $resource(destUrl);
$scope.ownerList = ownerResource.query(); $scope.ownerList = ownerResource.query();
}]); }]);
/* /*
...@@ -50,33 +50,43 @@ angular.module('controllers').controller('ownerDetailController', ['$scope', '$r ...@@ -50,33 +50,43 @@ angular.module('controllers').controller('ownerDetailController', ['$scope', '$r
*/ */
angular.module('controllers').controller('ownerFormController', ['$scope', '$http', '$resource', '$stateParams', '$state', angular.module('controllers').controller('ownerFormController', ['$scope', '$http', '$resource', '$stateParams', '$state',
function($scope, $http, $resource, $stateParams, $state) { function($scope, $http, $resource, $stateParams, $state) {
$scope.submitOwnerForm = {}; $scope.submitOwnerForm = {};
$scope.submitOwnerForm = function() { $scope.submitOwnerForm = function() {
var form = $scope.owner; var form = $scope.owner;
// Creating a Javascript object // Creating a Javascript object
var data = { var data = {
firstName: form.firstName, firstName: form.firstName,
lastName: form.lastName, lastName: form.lastName,
address: form.address, address: form.address,
city: form.city, city: form.city,
telephone: form.telephone telephone: form.telephone
}; };
if ($state.current.name == 'app.owneredit') { var request;
var restUrl = "/petclinic/owner/" + $stateParams.id;
$http.put(restUrl, data); if ($state.current.name == 'app.owneredit') {
$state.go('app.ownerlist'); var restUrl = "/petclinic/owner/" + $stateParams.id;
} request = $http.put(restUrl, data);
else { // in case of owner creation }
var restUrl = "/petclinic/owner"; else { // in case of owner creation
$http.post(restUrl, data); var restUrl = "/petclinic/owner";
$state.go('app.ownerlist'); request = $http.post(restUrl, data);
} }
request.then(function () {
$state.go('app.ownerlist');
}, function (response) {
var error = response.data;
alert(error.error + "\r\n" + error.errors.map(function (e) {
return e.field + ": " + e.defaultMessage;
}).join("\r\n"));
});
} }
if ($state.current.name == 'app.owneredit') { if ($state.current.name == 'app.owneredit') {
loadOwner($scope, $resource, $stateParams); loadOwner($scope, $resource, $stateParams);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment