AngularJS CRUD Grid v2: Now using $resource instead of $http

In V1 of my AngularJS, WebApi grid I used $http to call my WebApi endpoints. I’ve been using $resource a lot lately, so I thought I’d go back and update it to use $resource instead.

Here’s what it looks like:

SOURCE: https://github.com/jonbgallant/AngularJS-WebApi-EF

DEMO: http://angularjs-webapi-crud-grid.azurewebsites.net/

Here’s how I wired up resource:

1. Reference angular-resource.js

2. Inject ngResource into the app

1
var app = angular.module('app', ['ngResource']);

3. Modify the personFactory to return a resource object instead of the individual $http calls.

1
2
3
4
5
6
app.factory('personFactory', function ($http, $resource) {
return $resource('api/person/:id',
{ id: '@@id' },
{ 'update': { method: 'PUT' } },
{ 'query': { method: 'GET', isArray: false } });
});

4. Modify the $scope methods to use the resource methods, save, delete, query, update:

1
2
3
$scope.addPerson = function () {
personFactory.save($scope.person, successPostCallback, errorCallback);
};

Here’s the entire view. I’ll refactor it later.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<!doctype html>
<html ng-app="app">
<head>
<title>AngularJS-WebApi-EF</title>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/angular")
@Scripts.Render("~/bundles/toastr")
@Scripts.Render("~/bundles/bootstrap")
@Styles.Render("~/content/bootstrap")
@Styles.Render("~/content/toastr")
</head>
<body>
<div ng-controller="IndexCtrl" ng-cloak>
<table class="crud-grid table table-striped table-bordered table-condensed table-hover">
<tr>
<th style="width: 100px;">
<div class="btn-toolbar"><i class="btn icon-plus" ng-click="toggleAddMode()"></i></div>
</th>
<th style="width: 50px;">Id</th>
<th>Name</th>
</tr>
<tr ng-show="addMode">
<td>
<div class="btn-toolbar">
<div class="btn-group">
<i class="btn icon-save" ng-click="addPerson()"></i>
<i class="btn icon-remove" ng-click="toggleAddMode()"></i>
</div>
</div>
</td>
<td></td>
<td>
<input ng-model="person.Name" />
</td>
</tr>
<tr ng-repeat="person in people | orderBy:'Id':true">
<td>
<div class="btn-toolbar" ng-show="person.editMode == null || person.editMode == false">
<div class="btn-group">
<i class="btn icon-edit" ng-click="toggleEditMode(person)"></i>
<i class="btn icon-trash" ng-click="deletePerson(person)"></i>
</div>
</div>
<div class="btn-toolbar" ng-show="person.editMode == true">
<div class="btn-group">
<i class="btn icon-save" ng-click="updatePerson(person)"></i>
<i class="btn icon-remove" ng-click="toggleEditMode(person)"></i>
</div>
</div>
</td>
<td></td>
<td>
<span ng-show="person.editMode == null || person.editMode == false"></span>
<input ng-model="person.Name" ng-show="person.editMode == true" />
</td>
</tr>
</table>
</div>
<script type="text/javascript">
var app = angular.module('app', ['ngResource']);
app.factory('personFactory', function ($http, $resource) {
return $resource('api/person/:id',
{ id: '@@id' },
{ 'update': { method: 'PUT' } },
{ 'query': { method: 'GET', isArray: false } });
});
app.factory('notificationFactory', function () {
return {
success: function () {
toastr.success("Success");
},
error: function (text) {
toastr.error(text, "Error");
}
};
});
app.controller('IndexCtrl', function ($scope, $q, personFactory, notificationFactory) {
$scope.people = [];
$scope.addMode = false;
$scope.toggleAddMode = function () {
$scope.addMode = !$scope.addMode;
};
$scope.toggleEditMode = function (person) {
person.editMode = !person.editMode;
};
var successCallback = function (e, cb) {
notificationFactory.success();
$scope.getPeople(cb);
};
var successPostCallback = function (e) {
successCallback(e, function () {
$scope.toggleAddMode();
$scope.person = {};
});
};
var errorCallback = function (e) {
notificationFactory.error(e.data.Message);
};
$scope.addPerson = function () {
personFactory.save($scope.person, successPostCallback, errorCallback);
};
$scope.deletePerson = function (person) {
personFactory.delete({ id: person.Id }, successCallback, errorCallback);
};
$scope.updatePerson = function (person) {
personFactory.update({ id: person.Id }, person, successCallback, errorCallback);
};
$scope.getPeople = function (cb) {
personFactory.query(function (data) {
$scope.people = data;
if (cb) cb();
});
};
$scope.getPeople();
});
</script>
</body>
</html>

Share