dnet-applications/apps/dnet-is-application/src/main/resources/static/js/simpleResources.js

66 lines
1.7 KiB
JavaScript

var app = angular.module('resourcesApp', []);
app.controller('resourcesController', function($scope, $http) {
$scope.resources = [];
$scope.tmpRes = {};
$scope.mode = '';
$scope.reload = function() {
$http.get('./api/resources/?' + $.now()).then(function successCallback(res) {
$scope.resources = res.data;
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
});
};
$scope.prepareNewRes = function() {
$scope.mode = 'new';
$scope.tmpRes = {
'id' : '',
'name' : '',
'description' : ''
};
}
$scope.prepareEditRes = function(res) {
$scope.mode = 'edit';
$scope.tmpRes = angular.copy(res);
}
$scope.saveResource = function(res) {
if ($scope.mode == 'new') {
var found = false;
angular.forEach($scope.resources, function(r) {
if (res.id == r.id) { found = true; };
});
if (found) {
alert("Insertion failed: resource already exists !");
return;
}
}
$http.defaults.headers.post["Content-Type"] = "application/json;charset=UTF-8";
$http.post('./api/resources/?' + $.now(), res).then(function successCallback(res) {
$scope.resources = res.data;
alert("Resource saved");
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
});
};
$scope.deleteResource = function(id) {
if (confirm("Are you sure ?")) {
$http.delete('./api/resources/' + encodeURIComponent(id) + '?' + $.now()).then(function successCallback(res) {
$scope.resources = res.data;
alert("Resource deleted");
}, function errorCallback(res) {
alert('ERROR: ' + res.data.message);
});
}
};
$scope.reload();
});