Last active
August 29, 2015 14:03
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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