Skip to content

Instantly share code, notes, and snippets.

@aantipov
Created March 15, 2015 19:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aantipov/93360a0b0a47b216aa1c to your computer and use it in GitHub Desktop.
Save aantipov/93360a0b0a47b216aa1c to your computer and use it in GitHub Desktop.
Example of a form controller from angular.js project
angular.module('lk').controller('HikesFormCtrl', Controller);
function Controller(city, languagesData, hike, $state, $scope, $upload, settings, toastr, $http) {
var self = this;
var uploadedImageId;
self.isNew = !hike.id;
self.data = hike;
self.imageState = {
name: '',
loading: false
};
self.loading = false;
// Data for select lists.
self.languages = languagesData;
self.cities = []; // Filled runtime via self.getCities method when user type something.
// Some methods.
self.getCities = getCities;
self.upload = upload;
self.submit = submit;
self.cancel = cancel;
self.getImage = getImage;
$scope.$watch(
function () {
return self.files;
},
function () {
self.upload(self.files);
}
);
//////////////////
function submit() {
var successMsg = '<strong>Congratulations!</strong><br>Hike \'' + self.data.title + '\' has been successfully ';
successMsg += (self.isNew) ? 'created' : 'updated';
self.loading = true;
hike.$save().$asPromise()
// Save image if uploaded.
.then(function () {
if (uploadedImageId) {
return hike.saveImage(uploadedImageId);
}
return hike;
})
// Redirect to hike page if success
.then(function () {
$state.go('author.hikes.item.info', {id: hike.id})
.then(function () {
toastr.success(successMsg);
});
})
// Show errors
.catch(function () {
if (hike.$response.status === 401) {
return;
}
self.loading = false;
toastr.error('Some error occurred');
});
}
function cancel() {
$state.go('author.hikes.list');
}
function getCities(cityKey) {
if (!cityKey) {
return;
}
city.getList(cityKey).then(function (result) {
self.cities = result.geonames;
});
}
function upload(files) {
if (!files || !files.length) {
return;
}
self.data.image = null;
self.imageState.name = files[0].name;
self.imageState.loading = true;
$upload.upload({
url: settings.apiUrl + '/files/uploadImage',
headers: {'Authorization': 'Bearer ' + localStorage.getItem('auth_token')},
fileFormDataName: 'image',
file: files[0]
})
.progress(function (evt) {
console.log('My Progress!!!');
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
// TODO: Implement progress bar.
console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
})
.success(function (data) {
uploadedImageId = data.file;
self.imageState.loading = false;
toastr.success('Image \'' + files[0].name + '\' has been successfully uploaded');
})
.error(function (error, status) {
if (status === 401) {
return;
}
self.imageState.name = '';
self.imageState.loading = false;
toastr.error('Some error occurred when image \'' + files[0].name + '\' uploaded');
});
}
function getImage(hike) {
if (!hike) {
return settings.noImageUrl;
}
return settings.hikesImagesUrl + hike.id + '_1_thumb.jpg?ver=' + hike.imageVersion;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment