Skip to content

Instantly share code, notes, and snippets.

@elisechant
Last active August 29, 2015 14:17
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 elisechant/4786ec2beac0096d3aa4 to your computer and use it in GitHub Desktop.
Save elisechant/4786ec2beac0096d3aa4 to your computer and use it in GitHub Desktop.
Angular $resource CRUD operations with Factory Constructor model and data service
angular.module('app', [
'ngRoute',
'ngResource'
]);
angular.module('app')
.constant('API_URL', '/api');
angular.module('app').config(function($routeProvider) {
$routeProvider
.when('/books', {
templateUrl: 'templates/books-resource.html',
controller: 'BooksController',
controllerAs: 'booksCtrl'
})
.otherwise('/books')
});
angular.module('app').factory('BookModel', function() {
return function(book) {
this.title = book.title;
this.author = book.author || 'Anonymous';
this.year_published = book.year_published || 2015;
}
});
angular.module('app').factory('BooksResource', function(API_URL, $resource) {
var Books = $resource(API_URL + '/books/:book_id', {'book_id': '@book_id'}, {
update: {method: 'put'}
// extra custom methods go here
});
// Model computed properties - don't get stored on the model - instance methods and properties
Books.prototype.canRead = function() {
return true;
// 'this' is this model instance
//return this.isAdmin === true;
};
return Books;
});
angular.module('app').controller('BooksController', function(BooksResource, $scope) {
$scope.books = BooksResource.query(); // set books on $scope so bookCtrl has access
});
angular.module('app').controller('BookController', function(BooksResource, $scope, $routeParams, $location) {
var self = this;
if (!$scope.book) {
$scope.book = BooksResource.get({book_id: $routeParams.book_id});
}
self.isEditing = false;
self.updateBook = function() {
$scope.book.$update().then(function(resp) {
self.isEditing = false;
});
};
self.deleteBook = function(book) {
$scope.book.$delete().then(function(resp) {
if ($scope.books) {
$scope.books.splice($scope.books.indexOf($scope.book), 1);
} else {
$location.path('/books');
}
});
};
});
angular.module('app').controller('CreateBookController', function(BooksResource, BookModel, $scope, $location) {
var self = this;
self.form = {};
self.createBook = function() {
return new BooksResource(new BookModel(self.form)).$save().then(function(resp) {
if ($scope.books) {
$scope.books.push(resp);
} else {
$location.path('/books/' + resp.book_id);
}
});
};
});
<div ng-controller="CreateBookController as createBookCtrl">
<form name="createForm" novalidate ng-submit="createBookCtrl.createBook()">
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control"
ng-model="createBookCtrl.form.title"
name="title">
</div>
<div class="form-group">
<label>Author</label>
<input type="text" class="form-control"
ng-model="createBookCtrl.form.author"
name="author">
</div>
<div class="form-group">
<label>Year published</label>
<input type="text" class="form-control"
ng-model="createBookCtrl.form.year_published"
name="year_published">
</div>
<button type="submit">Create</button>
</form>
</div>
<div ng-repeat="book in books" ng-controller="BookController as bookCtrl">
<div ng-if="bookCtrl.isEditing !== true">
<div>{{book.book_id}} </div>
<div>{{book.title}}</div>
<div>{{book.author}}</div>
<div>{{book.year_published}}</div>
<div>
<button ng-click="bookCtrl.deleteBook(book)">Remove</button>
<button ng-click="bookCtrl.isEditing = true">Edit</button>
</div>
</div>
<div ng-if="bookCtrl.isEditing === true">
Editing..
<div><input type="text" ng-model="book.title"></div>
<div><input type="text" ng-model="book.author"></div>
<div><input type="text" ng-model="book.year_published"></div>
<div>
<button ng-click="bookCtrl.updateBook(book)">Update</button>
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment