Skip to content

Commit b645c67

Browse files
[angularjs] use module instead of global variable.
1 parent ccb4128 commit b645c67

File tree

5 files changed

+115
-106
lines changed

5 files changed

+115
-106
lines changed

architecture-examples/angularjs/js/app.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/*global angular */
2-
/*jshint unused:false */
3-
'use strict';
42

53
/**
64
* The main TodoMVC app module
75
*
86
* @type {angular.Module}
97
*/
10-
var todomvc = angular.module('todomvc', ['ngRoute'])
8+
angular.module('todomvc', ['ngRoute'])
119
.config(function ($routeProvider) {
10+
'use strict';
11+
1212
$routeProvider.when('/', {
1313
controller: 'TodoCtrl',
1414
templateUrl: 'todomvc-index.html'
Lines changed: 67 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,84 @@
1-
/*global todomvc, angular */
2-
'use strict';
1+
/*global angular */
32

43
/**
54
* The main controller for the app. The controller:
65
* - retrieves and persists the model via the todoStorage service
76
* - exposes the model to the template and provides event handlers
87
*/
9-
todomvc.controller('TodoCtrl', function TodoCtrl($scope, $routeParams, todoStorage, filterFilter) {
10-
var todos = $scope.todos = todoStorage.get();
11-
12-
$scope.newTodo = '';
13-
$scope.editedTodo = null;
14-
15-
$scope.$watch('todos', function (newValue, oldValue) {
16-
$scope.remainingCount = filterFilter(todos, { completed: false }).length;
17-
$scope.completedCount = todos.length - $scope.remainingCount;
18-
$scope.allChecked = !$scope.remainingCount;
19-
if (newValue !== oldValue) { // This prevents unneeded calls to the local storage
20-
todoStorage.put(todos);
21-
}
22-
}, true);
23-
24-
// Monitor the current route for changes and adjust the filter accordingly.
25-
$scope.$on('$routeChangeSuccess', function () {
26-
var status = $scope.status = $routeParams.status || '';
27-
28-
$scope.statusFilter = (status === 'active') ?
29-
{ completed: false } : (status === 'completed') ?
30-
{ completed: true } : null;
31-
});
8+
angular.module('todomvc')
9+
.controller('TodoCtrl', function TodoCtrl($scope, $routeParams, todoStorage, filterFilter) {
10+
'use strict';
11+
12+
var todos = $scope.todos = todoStorage.get();
3213

33-
$scope.addTodo = function () {
34-
var newTodo = $scope.newTodo.trim();
35-
if (!newTodo.length) {
36-
return;
37-
}
14+
$scope.newTodo = '';
15+
$scope.editedTodo = null;
3816

39-
todos.push({
40-
title: newTodo,
41-
completed: false
17+
$scope.$watch('todos', function (newValue, oldValue) {
18+
$scope.remainingCount = filterFilter(todos, { completed: false }).length;
19+
$scope.completedCount = todos.length - $scope.remainingCount;
20+
$scope.allChecked = !$scope.remainingCount;
21+
if (newValue !== oldValue) { // This prevents unneeded calls to the local storage
22+
todoStorage.put(todos);
23+
}
24+
}, true);
25+
26+
// Monitor the current route for changes and adjust the filter accordingly.
27+
$scope.$on('$routeChangeSuccess', function () {
28+
var status = $scope.status = $routeParams.status || '';
29+
30+
$scope.statusFilter = (status === 'active') ?
31+
{ completed: false } : (status === 'completed') ?
32+
{ completed: true } : null;
4233
});
4334

44-
$scope.newTodo = '';
45-
};
35+
$scope.addTodo = function () {
36+
var newTodo = $scope.newTodo.trim();
37+
if (!newTodo.length) {
38+
return;
39+
}
4640

47-
$scope.editTodo = function (todo) {
48-
$scope.editedTodo = todo;
49-
// Clone the original todo to restore it on demand.
50-
$scope.originalTodo = angular.extend({}, todo);
51-
};
41+
todos.push({
42+
title: newTodo,
43+
completed: false
44+
});
5245

53-
$scope.doneEditing = function (todo) {
54-
$scope.editedTodo = null;
55-
todo.title = todo.title.trim();
46+
$scope.newTodo = '';
47+
};
5648

57-
if (!todo.title) {
58-
$scope.removeTodo(todo);
59-
}
60-
};
49+
$scope.editTodo = function (todo) {
50+
$scope.editedTodo = todo;
51+
// Clone the original todo to restore it on demand.
52+
$scope.originalTodo = angular.extend({}, todo);
53+
};
6154

62-
$scope.revertEditing = function (todo) {
63-
todos[todos.indexOf(todo)] = $scope.originalTodo;
64-
$scope.doneEditing($scope.originalTodo);
65-
};
55+
$scope.doneEditing = function (todo) {
56+
$scope.editedTodo = null;
57+
todo.title = todo.title.trim();
6658

67-
$scope.removeTodo = function (todo) {
68-
todos.splice(todos.indexOf(todo), 1);
69-
};
59+
if (!todo.title) {
60+
$scope.removeTodo(todo);
61+
}
62+
};
7063

71-
$scope.clearCompletedTodos = function () {
72-
$scope.todos = todos = todos.filter(function (val) {
73-
return !val.completed;
74-
});
75-
};
64+
$scope.revertEditing = function (todo) {
65+
todos[todos.indexOf(todo)] = $scope.originalTodo;
66+
$scope.doneEditing($scope.originalTodo);
67+
};
7668

77-
$scope.markAll = function (completed) {
78-
todos.forEach(function (todo) {
79-
todo.completed = !completed;
80-
});
81-
};
82-
});
69+
$scope.removeTodo = function (todo) {
70+
todos.splice(todos.indexOf(todo), 1);
71+
};
72+
73+
$scope.clearCompletedTodos = function () {
74+
$scope.todos = todos = todos.filter(function (val) {
75+
return !val.completed;
76+
});
77+
};
78+
79+
$scope.markAll = function (completed) {
80+
todos.forEach(function (todo) {
81+
todo.completed = !completed;
82+
});
83+
};
84+
});
Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1-
/*global todomvc */
2-
'use strict';
1+
/*global angular */
32

43
/**
54
* Directive that executes an expression when the element it is applied to gets
65
* an `escape` keydown event.
76
*/
8-
todomvc.directive('todoEscape', function () {
9-
var ESCAPE_KEY = 27;
10-
return function (scope, elem, attrs) {
11-
elem.bind('keydown', function (event) {
12-
if (event.keyCode === ESCAPE_KEY) {
13-
scope.$apply(attrs.todoEscape);
14-
}
15-
});
16-
};
17-
});
7+
angular.module('todomvc')
8+
.directive('todoEscape', function () {
9+
'use strict';
10+
11+
var ESCAPE_KEY = 27;
12+
13+
return function (scope, elem, attrs) {
14+
elem.bind('keydown', function (event) {
15+
if (event.keyCode === ESCAPE_KEY) {
16+
scope.$apply(attrs.todoEscape);
17+
}
18+
});
19+
};
20+
});
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
/*global todomvc */
2-
'use strict';
1+
/*global angular */
32

43
/**
54
* Directive that places focus on the element it is applied to when the
65
* expression it binds to evaluates to true
76
*/
8-
todomvc.directive('todoFocus', function todoFocus($timeout) {
9-
return function (scope, elem, attrs) {
10-
scope.$watch(attrs.todoFocus, function (newVal) {
11-
if (newVal) {
12-
$timeout(function () {
13-
elem[0].focus();
14-
}, 0, false);
15-
}
16-
});
17-
};
18-
});
7+
angular.module('todomvc')
8+
.directive('todoFocus', function todoFocus($timeout) {
9+
'use strict';
10+
11+
return function (scope, elem, attrs) {
12+
scope.$watch(attrs.todoFocus, function (newVal) {
13+
if (newVal) {
14+
$timeout(function () {
15+
elem[0].focus();
16+
}, 0, false);
17+
}
18+
});
19+
};
20+
});
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
/*global todomvc */
2-
'use strict';
1+
/*global angular */
32

43
/**
54
* Services that persists and retrieves TODOs from localStorage
65
*/
7-
todomvc.factory('todoStorage', function () {
8-
var STORAGE_ID = 'todos-angularjs';
6+
angular.module('todomvc')
7+
.factory('todoStorage', function () {
8+
'use strict';
99

10-
return {
11-
get: function () {
12-
return JSON.parse(localStorage.getItem(STORAGE_ID) || '[]');
13-
},
10+
var STORAGE_ID = 'todos-angularjs';
1411

15-
put: function (todos) {
16-
localStorage.setItem(STORAGE_ID, JSON.stringify(todos));
17-
}
18-
};
19-
});
12+
return {
13+
get: function () {
14+
return JSON.parse(localStorage.getItem(STORAGE_ID) || '[]');
15+
},
16+
17+
put: function (todos) {
18+
localStorage.setItem(STORAGE_ID, JSON.stringify(todos));
19+
}
20+
};
21+
});

0 commit comments

Comments
 (0)