Skip to content
Browse files

Refactored module definitions

  • Loading branch information...
1 parent c0256ac commit 7952b96c24b6fd9c751b1846d0d3cc2aa111e30b @mrako committed Jun 24, 2014
View
2 public/index.html
@@ -37,8 +37,8 @@ <h3 class="text-muted">Todo App</h3>
<script src="/components/angular-xeditable/dist/js/xeditable.js"></script>
+ <script src="/js/app.js"></script>
<script src="/js/controllers/todo_controller.js"></script>
<script src="/js/services/todo_factory.js"></script>
- <script src="/js/app.js"></script>
</body>
</html>
View
2 public/js/app.js
@@ -1,6 +1,6 @@
'use strict';
-var todoApp = angular.module('todoApp', ['TodoController', 'TodoFactory', 'xeditable']);
+var todoApp = angular.module('todoApp', ['xeditable']);
todoApp.run(function(editableOptions) {
editableOptions.theme = 'bs3'; // bootstrap3 theme. Can be also 'bs2', 'default'
View
115 public/js/controllers/todo_controller.js
@@ -1,71 +1,70 @@
'use strict';
-angular.module('TodoController', [])
- .controller('todoController', function($scope, $http, Todo) {
+angular.module('todoApp').controller('TodoController', function($scope, $http, todoService) {
+ $scope.formData = {};
+ $scope.submit = 'Create';
+ $scope.loading = true;
+
+ // GET =====================================================================
+ todoService.list()
+ .success(function(data) {
+ $scope.todos = data;
+ $scope.loading = false;
+ });
+
+ // CREATE AND SAVE =========================================================
+ $scope.save = function() {
+ $scope.loading = true;
+
+ if ($scope.formData._id === undefined) {
+ todoService.create($scope.formData)
+ .success(function(data) {
+ $scope.todos = data;
+ });
+ } else {
+ todoService.update($scope.formData._id, $scope.formData)
+ .success(function(data) {
+ $scope.todos = data;
+ });
+ }
+
+ $scope.loading = false;
$scope.formData = {};
$scope.submit = 'Create';
- $scope.loading = true;
+ };
- // GET =====================================================================
- Todo.list()
+ // CREATE ==================================================================
+ $scope.edit = function (id) {
+ todoService.get(id)
.success(function(data) {
- $scope.todos = data;
+ $scope.formData = data;
+ $scope.submit = 'Save';
$scope.loading = false;
});
+ };
- // CREATE AND SAVE =========================================================
- $scope.save = function() {
- $scope.loading = true;
-
- if ($scope.formData._id === undefined) {
- Todo.create($scope.formData)
- .success(function(data) {
- $scope.todos = data;
- });
- } else {
- Todo.update($scope.formData._id, $scope.formData)
- .success(function(data) {
- $scope.todos = data;
- });
- }
-
- $scope.loading = false;
- $scope.formData = {};
- $scope.submit = 'Create';
- };
-
- // CREATE ==================================================================
- $scope.edit = function (id) {
- Todo.get(id)
- .success(function(data) {
- $scope.formData = data;
- $scope.submit = 'Save';
- $scope.loading = false;
- });
- };
-
- // UPDATE ==================================================================
- $scope.update = function(todo) {
- $scope.updateData(todo, todo.text);
- };
+ // UPDATE ==================================================================
+ $scope.update = function(todo) {
+ $scope.updateData(todo, todo.text);
+ };
- $scope.updateData = function(todo, data) {
- $scope.loading = true;
+ $scope.updateData = function(todo, data) {
+ $scope.loading = true;
- Todo.update(todo._id, {done: todo.done, text: data})
- .success(function(data) {
- $scope.todos = data;
- });
- };
+ todoService.update(todo._id, {done: todo.done, text: data})
+ .success(function(data) {
+ $scope.todos = data;
+ });
+ };
- // DELETE ==================================================================
- $scope.delete = function(id) {
- $scope.loading = true;
+ // DELETE ==================================================================
+ $scope.delete = function(id) {
+ $scope.loading = true;
- Todo.delete(id)
- .success(function(data) {
- $scope.loading = false;
- $scope.todos = data;
- });
- };
- });
+ todoService.delete(id)
+ .success(function(data) {
+ $scope.loading = false;
+ $scope.todos = data;
+ });
+ };
+});
View
39 public/js/services/todo_factory.js
@@ -1,23 +1,22 @@
'use strict';
// SERVICE FOR API CALLS =======================================================
-angular.module('TodoFactory', [])
- .factory('Todo', function ($http) {
- return {
- list : function() {
- return $http.get('/api/todos');
- },
- get : function(id) {
- return $http.get('/api/todos/' + id);
- },
- create : function(data) {
- return $http.post('/api/todos', data);
- },
- update : function(id, data) {
- return $http.put('/api/todos/' + id, data);
- },
- delete : function(id) {
- return $http.delete('/api/todos/' + id);
- }
- };
- });
+angular.module('todoApp').factory('todoService', function ($http) {
+ return {
+ list : function() {
+ return $http.get('/api/todos');
+ },
+ get : function(id) {
+ return $http.get('/api/todos/' + id);
+ },
+ create : function(data) {
+ return $http.post('/api/todos', data);
+ },
+ update : function(id, data) {
+ return $http.put('/api/todos/' + id, data);
+ },
+ delete : function(id) {
+ return $http.delete('/api/todos/' + id);
+ }
+ };
+});
View
2 public/templates/todos.html
@@ -1,4 +1,4 @@
-<div ng-controller="todoController">
+<div ng-controller="TodoController">
<div class="jumbotron">
<h3>You currently have <ng-pluralize count="todos.length" when="{'0': 'no todos', '1': '1 todo', 'other': '{} todos'}" /></h3>

0 comments on commit 7952b96

Please sign in to comment.
Something went wrong with that request. Please try again.