Skip to content

Commit 26bfec3

Browse files
kaidokertsamccone
authored andcommitted
Switch to angular-resource
1 parent 6cf92ac commit 26bfec3

File tree

3 files changed

+22
-31
lines changed

3 files changed

+22
-31
lines changed

examples/angularjs/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ <h1>todos</h1>
6666
<script src="node_modules/todomvc-common/base.js"></script>
6767
<script src="node_modules/angular/angular.js"></script>
6868
<script src="node_modules/angular-route/angular-route.js"></script>
69+
<script src="node_modules/angular-resource/angular-resource.js"></script>
6970
<script src="js/app.js"></script>
7071
<script src="js/controllers/todoCtrl.js"></script>
7172
<script src="js/services/todoStorage.js"></script>

examples/angularjs/js/app.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* @type {angular.Module}
77
*/
8-
angular.module('todomvc', ['ngRoute'])
8+
angular.module('todomvc', ['ngRoute', 'ngResource'])
99
.config(function ($routeProvider) {
1010
'use strict';
1111

examples/angularjs/js/services/todoStorage.js

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,18 @@ angular.module('todomvc')
2121
});
2222
})
2323

24-
.factory('api', function ($http) {
24+
.factory('api', function ($resource) {
2525
'use strict';
2626

2727
var store = {
2828
todos: [],
2929

30+
api: $resource('/api/todos/:id', null,
31+
{
32+
update: { method:'PUT' }
33+
}
34+
),
35+
3036
clearCompleted: function () {
3137
var originalTodos = store.todos.slice(0);
3238

@@ -42,61 +48,45 @@ angular.module('todomvc')
4248

4349
angular.copy(incompleteTodos, store.todos);
4450

45-
return $http.delete('/api/todos')
46-
.then(function success() {
47-
return store.todos;
51+
return store.api.delete(function () {
4852
}, function error() {
4953
angular.copy(originalTodos, store.todos);
50-
return originalTodos;
5154
});
5255
},
5356

5457
delete: function (todo) {
5558
var originalTodos = store.todos.slice(0);
5659

5760
store.todos.splice(store.todos.indexOf(todo), 1);
58-
59-
return $http.delete('/api/todos/' + todo.id)
60-
.then(function success() {
61-
return store.todos;
61+
return store.api.delete({ id: todo.id },
62+
function () {
6263
}, function error() {
6364
angular.copy(originalTodos, store.todos);
64-
return originalTodos;
6565
});
6666
},
6767

6868
get: function () {
69-
return $http.get('/api/todos')
70-
.then(function (resp) {
71-
angular.copy(resp.data, store.todos);
72-
return store.todos;
73-
});
69+
return store.api.query(function (resp) {
70+
angular.copy(resp, store.todos);
71+
});
7472
},
7573

7674
insert: function (todo) {
7775
var originalTodos = store.todos.slice(0);
7876

79-
return $http.post('/api/todos', todo)
80-
.then(function success(resp) {
81-
todo.id = resp.data.id;
77+
return store.api.save(todo,
78+
function success(resp) {
79+
todo.id = resp.id;
8280
store.todos.push(todo);
83-
return store.todos;
8481
}, function error() {
8582
angular.copy(originalTodos, store.todos);
86-
return store.todos;
87-
});
83+
})
84+
.$promise;
8885
},
8986

9087
put: function (todo) {
91-
var originalTodos = store.todos.slice(0);
92-
93-
return $http.put('/api/todos/' + todo.id, todo)
94-
.then(function success() {
95-
return store.todos;
96-
}, function error() {
97-
angular.copy(originalTodos, store.todos);
98-
return originalTodos;
99-
});
88+
return store.api.update({ id: todo.id }, todo)
89+
.$promise;
10090
}
10191
};
10292

0 commit comments

Comments
 (0)