Commit 26bfec34 authored by Kaido Kert's avatar Kaido Kert Committed by Sam Saccone

Switch to angular-resource

parent 6cf92ac3
...@@ -66,6 +66,7 @@ ...@@ -66,6 +66,7 @@
<script src="node_modules/todomvc-common/base.js"></script> <script src="node_modules/todomvc-common/base.js"></script>
<script src="node_modules/angular/angular.js"></script> <script src="node_modules/angular/angular.js"></script>
<script src="node_modules/angular-route/angular-route.js"></script> <script src="node_modules/angular-route/angular-route.js"></script>
<script src="node_modules/angular-resource/angular-resource.js"></script>
<script src="js/app.js"></script> <script src="js/app.js"></script>
<script src="js/controllers/todoCtrl.js"></script> <script src="js/controllers/todoCtrl.js"></script>
<script src="js/services/todoStorage.js"></script> <script src="js/services/todoStorage.js"></script>
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
* *
* @type {angular.Module} * @type {angular.Module}
*/ */
angular.module('todomvc', ['ngRoute']) angular.module('todomvc', ['ngRoute', 'ngResource'])
.config(function ($routeProvider) { .config(function ($routeProvider) {
'use strict'; 'use strict';
......
...@@ -21,12 +21,18 @@ angular.module('todomvc') ...@@ -21,12 +21,18 @@ angular.module('todomvc')
}); });
}) })
.factory('api', function ($http) { .factory('api', function ($resource) {
'use strict'; 'use strict';
var store = { var store = {
todos: [], todos: [],
api: $resource('/api/todos/:id', null,
{
update: { method:'PUT' }
}
),
clearCompleted: function () { clearCompleted: function () {
var originalTodos = store.todos.slice(0); var originalTodos = store.todos.slice(0);
...@@ -42,12 +48,9 @@ angular.module('todomvc') ...@@ -42,12 +48,9 @@ angular.module('todomvc')
angular.copy(incompleteTodos, store.todos); angular.copy(incompleteTodos, store.todos);
return $http.delete('/api/todos') return store.api.delete(function () {
.then(function success() {
return store.todos;
}, function error() { }, function error() {
angular.copy(originalTodos, store.todos); angular.copy(originalTodos, store.todos);
return originalTodos;
}); });
}, },
...@@ -55,48 +58,35 @@ angular.module('todomvc') ...@@ -55,48 +58,35 @@ angular.module('todomvc')
var originalTodos = store.todos.slice(0); var originalTodos = store.todos.slice(0);
store.todos.splice(store.todos.indexOf(todo), 1); store.todos.splice(store.todos.indexOf(todo), 1);
return store.api.delete({ id: todo.id },
return $http.delete('/api/todos/' + todo.id) function () {
.then(function success() {
return store.todos;
}, function error() { }, function error() {
angular.copy(originalTodos, store.todos); angular.copy(originalTodos, store.todos);
return originalTodos;
}); });
}, },
get: function () { get: function () {
return $http.get('/api/todos') return store.api.query(function (resp) {
.then(function (resp) { angular.copy(resp, store.todos);
angular.copy(resp.data, store.todos); });
return store.todos;
});
}, },
insert: function (todo) { insert: function (todo) {
var originalTodos = store.todos.slice(0); var originalTodos = store.todos.slice(0);
return $http.post('/api/todos', todo) return store.api.save(todo,
.then(function success(resp) { function success(resp) {
todo.id = resp.data.id; todo.id = resp.id;
store.todos.push(todo); store.todos.push(todo);
return store.todos;
}, function error() { }, function error() {
angular.copy(originalTodos, store.todos); angular.copy(originalTodos, store.todos);
return store.todos; })
}); .$promise;
}, },
put: function (todo) { put: function (todo) {
var originalTodos = store.todos.slice(0); return store.api.update({ id: todo.id }, todo)
.$promise;
return $http.put('/api/todos/' + todo.id, todo)
.then(function success() {
return store.todos;
}, function error() {
angular.copy(originalTodos, store.todos);
return originalTodos;
});
} }
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment