Commit 6f8b0d9b authored by dmitriz's avatar dmitriz

angularjs-perf: Simplified functions

parent 8d40a1c0
......@@ -12,21 +12,21 @@
<header id="header">
<h1>todos</h1>
<form id="todo-form" ng-submit="TC.addTodo()">
<input id="new-todo" placeholder="What needs to be done?" ng-model="TC.newTodo" autofocus>
<input id="new-todo" placeholder="What needs to be done?" ng-model="TC.newTodo.title" autofocus>
</form>
</header>
<section id="main" ng-show="TC.todos.length" ng-cloak>
<input id="toggle-all" type="checkbox" ng-model="TC.allChecked" ng-click="TC.markAll(TC.allChecked)">
<label for="toggle-all">Mark all as complete</label>
<ul id="todo-list">
<li ng-repeat="todo in TC.todos | filter:TC.statusFilter track by $index" ng-class="{completed: todo.completed, editing: todo == TC.editedTodo}">
<li ng-repeat="todo in TC.todos | filter:TC.statusFilter track by $index" ng-class="{completed: todo.completed, editing: todo === TC.editedTodo}">
<div class="view">
<input class="toggle" type="checkbox" ng-model="todo.completed" ng-change="TC.todoCompleted(todo)">
<input class="toggle" type="checkbox" ng-model="todo.completed">
<label ng-dblclick="TC.editTodo(todo)">{{todo.title}}</label>
<button class="destroy" ng-click="TC.removeTodo(todo)"></button>
<button class="destroy" ng-click="TC.removeTodo($index)"></button>
</div>
<form ng-submit="TC.doneEditing(todo)">
<input class="edit" ng-trim="false" ng-model="todo.title" ng-blur="TC.doneEditing(todo)" todo-escape="TC.revertEditing(todo)" todo-focus="todo == TC.editedTodo">
<form ng-submit="TC.doneEditing(todo, $index)">
<input class="edit" ng-trim="false" ng-model="todo.title" ng-blur="TC.doneEditing(todo, $index)" todo-escape="TC.revertEditing($index)" todo-focus="todo === TC.editedTodo">
</form>
</li>
</ul>
......
......@@ -9,13 +9,13 @@
* - retrieves and persists the model via the todoStorage service
* - exposes the model to the template and provides event handlers
*/
.controller('TodoCtrl', function TodoCtrl($scope, $location, $filter, todoStorage) {
.controller('TodoCtrl', function TodoCtrl($scope, $location, todoStorage) {
var TC = this;
var todos = TC.todos = todoStorage.get();
TC.newTodo = '';
TC.remainingCount = $filter('filter')(todos, {completed: false}).length;
TC.editedTodo = null;
TC.newTodo = {title: '', completed: false};
TC.editedTodo = {};
if ($location.path() === '') {
$location.path('/');
......@@ -27,73 +27,61 @@
TC.statusFilter = { '/active': {completed: false}, '/completed': {completed: true} }[path];
});
$scope.$watch('TC.remainingCount == 0', function (val) {
TC.allChecked = val;
});
// 3rd argument `true` for deep object watching
$scope.$watch('TC.todos', function () {
TC.remainingCount = todos.filter(function (todo) { return !todo.completed; }).length;
TC.allChecked = (TC.remainingCount === 0);
// Save any changes to localStorage
todoStorage.put(todos);
}, true);
TC.addTodo = function () {
var newTodo = TC.newTodo.trim();
if (newTodo.length === 0) {
var newTitle = TC.newTodo.title = TC.newTodo.title.trim();
if (newTitle.length === 0) {
return;
}
todos.push({
title: newTodo,
completed: false
});
todoStorage.put(todos);
todos.push(TC.newTodo);
TC.newTodo = '';
TC.remainingCount++;
TC.newTodo = {title: '', completed: false};
};
TC.editTodo = function (todo) {
TC.editedTodo = todo;
// Clone the original todo to restore it on demand.
TC.originalTodo = angular.extend({}, todo);
TC.originalTodo = angular.copy(todo);
};
TC.doneEditing = function (todo) {
TC.editedTodo = null;
TC.doneEditing = function (todo, index) {
TC.editedTodo = {};
todo.title = todo.title.trim();
if (!todo.title) {
TC.removeTodo(todo);
TC.removeTodo(index);
}
todoStorage.put(todos);
};
TC.revertEditing = function (todo) {
todos[todos.indexOf(todo)] = TC.originalTodo;
TC.revertEditing = function (index) {
todos[index] = TC.originalTodo;
TC.doneEditing(TC.originalTodo);
};
TC.removeTodo = function (todo) {
TC.remainingCount -= todo.completed ? 0 : 1;
todos.splice(todos.indexOf(todo), 1);
todoStorage.put(todos);
};
TC.todoCompleted = function (todo) {
TC.remainingCount += todo.completed ? -1 : 1;
todoStorage.put(todos);
TC.removeTodo = function (index) {
todos.splice(index, 1);
};
TC.clearCompletedTodos = function () {
TC.todos = todos = todos.filter(function (val) {
return !val.completed;
});
todoStorage.put(todos);
};
TC.markAll = function (completed) {
todos.forEach(function (todo) {
todo.completed = completed;
});
TC.remainingCount = completed ? 0 : todos.length;
todoStorage.put(todos);
};
});
})();
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