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

Switch to angular-resource

parent 6cf92ac3
......@@ -66,6 +66,7 @@
<script src="node_modules/todomvc-common/base.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-resource/angular-resource.js"></script>
<script src="js/app.js"></script>
<script src="js/controllers/todoCtrl.js"></script>
<script src="js/services/todoStorage.js"></script>
......
......@@ -5,7 +5,7 @@
*
* @type {angular.Module}
*/
angular.module('todomvc', ['ngRoute'])
angular.module('todomvc', ['ngRoute', 'ngResource'])
.config(function ($routeProvider) {
'use strict';
......
......@@ -21,12 +21,18 @@ angular.module('todomvc')
});
})
.factory('api', function ($http) {
.factory('api', function ($resource) {
'use strict';
var store = {
todos: [],
api: $resource('/api/todos/:id', null,
{
update: { method:'PUT' }
}
),
clearCompleted: function () {
var originalTodos = store.todos.slice(0);
......@@ -42,12 +48,9 @@ angular.module('todomvc')
angular.copy(incompleteTodos, store.todos);
return $http.delete('/api/todos')
.then(function success() {
return store.todos;
return store.api.delete(function () {
}, function error() {
angular.copy(originalTodos, store.todos);
return originalTodos;
});
},
......@@ -55,48 +58,35 @@ angular.module('todomvc')
var originalTodos = store.todos.slice(0);
store.todos.splice(store.todos.indexOf(todo), 1);
return $http.delete('/api/todos/' + todo.id)
.then(function success() {
return store.todos;
return store.api.delete({ id: todo.id },
function () {
}, function error() {
angular.copy(originalTodos, store.todos);
return originalTodos;
});
},
get: function () {
return $http.get('/api/todos')
.then(function (resp) {
angular.copy(resp.data, store.todos);
return store.todos;
});
return store.api.query(function (resp) {
angular.copy(resp, store.todos);
});
},
insert: function (todo) {
var originalTodos = store.todos.slice(0);
return $http.post('/api/todos', todo)
.then(function success(resp) {
todo.id = resp.data.id;
return store.api.save(todo,
function success(resp) {
todo.id = resp.id;
store.todos.push(todo);
return store.todos;
}, function error() {
angular.copy(originalTodos, store.todos);
return store.todos;
});
})
.$promise;
},
put: function (todo) {
var originalTodos = store.todos.slice(0);
return $http.put('/api/todos/' + todo.id, todo)
.then(function success() {
return store.todos;
}, function error() {
angular.copy(originalTodos, store.todos);
return originalTodos;
});
return store.api.update({ id: todo.id }, todo)
.$promise;
}
};
......
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