mirror of
https://git.victorphan.net/basketballcantho/Teedy_custom.git
synced 2026-08-06 06:13:10 +07:00
Edit document, add files, display files
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
/**
|
||||
* Document edition controller.
|
||||
*/
|
||||
App.controller('DocumentEdit', function($scope, $state, $stateParams, Restangular) {
|
||||
App.controller('DocumentEdit', function($scope, $http, $state, $stateParams, Restangular) {
|
||||
/**
|
||||
* Returns true if in edit mode (false in add mode).
|
||||
*/
|
||||
@@ -11,27 +11,65 @@ App.controller('DocumentEdit', function($scope, $state, $stateParams, Restangula
|
||||
return $stateParams.id;
|
||||
};
|
||||
|
||||
/**
|
||||
* In edit mode, load the current document.
|
||||
*/
|
||||
if ($scope.isEdit()) {
|
||||
Restangular.one('document', $stateParams.id).get().then(function(data) {
|
||||
$scope.document = data;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a document.
|
||||
*/
|
||||
$scope.edit = function() {
|
||||
var promise = null;
|
||||
|
||||
if ($scope.isEdit()) {
|
||||
// TODO
|
||||
promise = Restangular
|
||||
.one('document', $stateParams.id)
|
||||
.post('', $scope.document);
|
||||
promise.then(function(data) {
|
||||
$scope.loadDocuments();
|
||||
$state.transitionTo('document.view', { id: $stateParams.id });
|
||||
})
|
||||
} else {
|
||||
Restangular
|
||||
promise = Restangular
|
||||
.one('document')
|
||||
.put($scope.document)
|
||||
.then(function() {
|
||||
.put($scope.document);
|
||||
promise.then(function(data) {
|
||||
$scope.document = {};
|
||||
$scope.loadDocuments();
|
||||
});
|
||||
}
|
||||
|
||||
// Upload files after edition
|
||||
// TODO Handle file upload progression and errors
|
||||
promise.then(function(data) {
|
||||
_.each($scope.files, function(file) {
|
||||
var formData = new FormData();
|
||||
formData.append('id', data.id);
|
||||
formData.append('file', file);
|
||||
$.ajax({
|
||||
url: 'api/file',
|
||||
type: 'PUT',
|
||||
data: formData,
|
||||
processData: false,
|
||||
contentType: false
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Cancel edition.
|
||||
*/
|
||||
$scope.cancel = function() {
|
||||
$state.transitionTo('document');
|
||||
if ($scope.isEdit()) {
|
||||
$state.transitionTo('document.view', { id: $stateParams.id });
|
||||
} else {
|
||||
$state.transitionTo('document.default');
|
||||
}
|
||||
};
|
||||
});
|
||||
Reference in New Issue
Block a user