Skip to content

Instantly share code, notes, and snippets.

@mcranston18
Last active August 29, 2015 14:03
Show Gist options
  • Save mcranston18/1d8cb313b8f30b60f9af to your computer and use it in GitHub Desktop.
Save mcranston18/1d8cb313b8f30b60f9af to your computer and use it in GitHub Desktop.
This is a sample Angular controller demonstrating a possible way to organize your CRUD operations.
'use strict';
angular.module('app.photo')
.controller('EditPhotoCtrl', function($scope, $state, $location, $window,
UserService, PhotoService, UtilitiesService) {
/**
* INIT VALUES
*/
var ctrl = this;
var utils = UtilitiesService;
/**
* PRIVATE METHODS
* methods used only in ctrl
*/
ctrl._resizePhoto = function() { ... }
/**
* METHODS USED IN VIEW
*/
ctrl.showSomething = function(arg) { ... }
ctrl.clearForm = function() { ... }
ctrl.resetForm = function() { ... }
/**
* CRUD
*/
ctrl.update = {
success: function(data) {
$scope.photo = data;
$scope.loading = false;
$location.path('/photo/' + $scope.photo.id);
},
error: function(error) {
$scope.loading = false;
},
invoke: function(photo, id) {
$scope.loading = true;
var formData = utils.createFormData(photo);
PhotoService.updatePhoto(formData, id)
.then(this.success, this.error);
}
};
ctrl.get = {
success: function(data) {
$scope.photos = data;
},
invoke: function() {
UserService.getAccount()
.then(this.success);
}
};
ctrl.get.invoke();
/**
* WATCHES
*/
$scope.$watch('something', function() { ... });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment