Commit 8d40a1c0 authored by dmitriz's avatar dmitriz

angularjs-perf: Change to `Controller as` syntax

parent 81897ce5
...@@ -8,45 +8,45 @@ ...@@ -8,45 +8,45 @@
<style>[ng-cloak] { display: none; }</style> <style>[ng-cloak] { display: none; }</style>
</head> </head>
<body> <body>
<section id="todoapp" ng-controller="TodoCtrl"> <section id="todoapp" ng-controller="TodoCtrl as TC">
<header id="header"> <header id="header">
<h1>todos</h1> <h1>todos</h1>
<form id="todo-form" ng-submit="addTodo()"> <form id="todo-form" ng-submit="TC.addTodo()">
<input id="new-todo" placeholder="What needs to be done?" ng-model="newTodo" autofocus> <input id="new-todo" placeholder="What needs to be done?" ng-model="TC.newTodo" autofocus>
</form> </form>
</header> </header>
<section id="main" ng-show="todos.length" ng-cloak> <section id="main" ng-show="TC.todos.length" ng-cloak>
<input id="toggle-all" type="checkbox" ng-model="allChecked" ng-click="markAll(allChecked)"> <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> <label for="toggle-all">Mark all as complete</label>
<ul id="todo-list"> <ul id="todo-list">
<li ng-repeat="todo in todos | filter:statusFilter track by $index" ng-class="{completed: todo.completed, editing: todo == 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"> <div class="view">
<input class="toggle" type="checkbox" ng-model="todo.completed" ng-change="todoCompleted(todo)"> <input class="toggle" type="checkbox" ng-model="todo.completed" ng-change="TC.todoCompleted(todo)">
<label ng-dblclick="editTodo(todo)">{{todo.title}}</label> <label ng-dblclick="TC.editTodo(todo)">{{todo.title}}</label>
<button class="destroy" ng-click="removeTodo(todo)"></button> <button class="destroy" ng-click="TC.removeTodo(todo)"></button>
</div> </div>
<form ng-submit="doneEditing(todo)"> <form ng-submit="TC.doneEditing(todo)">
<input class="edit" ng-trim="false" ng-model="todo.title" ng-blur="doneEditing(todo)" todo-escape="revertEditing(todo)" todo-focus="todo == editedTodo"> <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> </form>
</li> </li>
</ul> </ul>
</section> </section>
<footer id="footer" ng-show="todos.length" ng-cloak> <footer id="footer" ng-show="TC.todos.length" ng-cloak>
<span id="todo-count"><strong>{{remainingCount}}</strong> <span id="todo-count"><strong>{{TC.remainingCount}}</strong>
<ng-pluralize count="remainingCount" when="{ one: 'item left', other: 'items left' }"></ng-pluralize> <ng-pluralize count="TC.remainingCount" when="{ one: 'item left', other: 'items left' }"></ng-pluralize>
</span> </span>
<ul id="filters"> <ul id="filters">
<li> <li>
<a ng-class="{selected: location.path() == '/'} " href="#/">All</a> <a ng-class="{selected: TC.location.path() == '/'} " href="#/">All</a>
</li> </li>
<li> <li>
<a ng-class="{selected: location.path() == '/active'}" href="#/active">Active</a> <a ng-class="{selected: TC.location.path() == '/active'}" href="#/active">Active</a>
</li> </li>
<li> <li>
<a ng-class="{selected: location.path() == '/completed'}" href="#/completed">Completed</a> <a ng-class="{selected: TC.location.path() == '/completed'}" href="#/completed">Completed</a>
</li> </li>
</ul> </ul>
<button id="clear-completed" ng-click="clearCompletedTodos()" ng-show="remainingCount < todos.length">Clear completed</button> <button id="clear-completed" ng-click="TC.clearCompletedTodos()" ng-show="TC.remainingCount < TC.todos.length">Clear completed</button>
</footer> </footer>
</section> </section>
<footer id="info"> <footer id="info">
......
...@@ -8,5 +8,5 @@ ...@@ -8,5 +8,5 @@
* *
* @type {angular.Module} * @type {angular.Module}
*/ */
angular.module('todomvc', ['todoCtrl', 'todoEscape', 'todoStorage']); angular.module('todomvc', ['todoCtrl', 'todoEscape', 'todoFocus', 'todoStorage']);
})(); })();
...@@ -10,28 +10,29 @@ ...@@ -10,28 +10,29 @@
* - exposes the model to the template and provides event handlers * - exposes the model to the template and provides event handlers
*/ */
.controller('TodoCtrl', function TodoCtrl($scope, $location, $filter, todoStorage) { .controller('TodoCtrl', function TodoCtrl($scope, $location, $filter, todoStorage) {
var todos = $scope.todos = todoStorage.get(); var TC = this;
var todos = TC.todos = todoStorage.get();
$scope.newTodo = ''; TC.newTodo = '';
$scope.remainingCount = $filter('filter')(todos, {completed: false}).length; TC.remainingCount = $filter('filter')(todos, {completed: false}).length;
$scope.editedTodo = null; TC.editedTodo = null;
if ($location.path() === '') { if ($location.path() === '') {
$location.path('/'); $location.path('/');
} }
$scope.location = $location; TC.location = $location;
$scope.$watch('location.path()', function (path) { $scope.$watch('TC.location.path()', function (path) {
$scope.statusFilter = { '/active': {completed: false}, '/completed': {completed: true} }[path]; TC.statusFilter = { '/active': {completed: false}, '/completed': {completed: true} }[path];
}); });
$scope.$watch('remainingCount == 0', function (val) { $scope.$watch('TC.remainingCount == 0', function (val) {
$scope.allChecked = val; TC.allChecked = val;
}); });
$scope.addTodo = function () { TC.addTodo = function () {
var newTodo = $scope.newTodo.trim(); var newTodo = TC.newTodo.trim();
if (newTodo.length === 0) { if (newTodo.length === 0) {
return; return;
} }
...@@ -42,55 +43,56 @@ ...@@ -42,55 +43,56 @@
}); });
todoStorage.put(todos); todoStorage.put(todos);
$scope.newTodo = ''; TC.newTodo = '';
$scope.remainingCount++; TC.remainingCount++;
}; };
$scope.editTodo = function (todo) { TC.editTodo = function (todo) {
$scope.editedTodo = todo; TC.editedTodo = todo;
// Clone the original todo to restore it on demand. // Clone the original todo to restore it on demand.
$scope.originalTodo = angular.extend({}, todo); TC.originalTodo = angular.extend({}, todo);
}; };
$scope.doneEditing = function (todo) { TC.doneEditing = function (todo) {
$scope.editedTodo = null; TC.editedTodo = null;
todo.title = todo.title.trim(); todo.title = todo.title.trim();
if (!todo.title) { if (!todo.title) {
$scope.removeTodo(todo); TC.removeTodo(todo);
} }
todoStorage.put(todos); todoStorage.put(todos);
}; };
$scope.revertEditing = function (todo) { TC.revertEditing = function (todo) {
todos[todos.indexOf(todo)] = $scope.originalTodo; todos[todos.indexOf(todo)] = TC.originalTodo;
$scope.doneEditing($scope.originalTodo); TC.doneEditing(TC.originalTodo);
}; };
$scope.removeTodo = function (todo) { TC.removeTodo = function (todo) {
$scope.remainingCount -= todo.completed ? 0 : 1; TC.remainingCount -= todo.completed ? 0 : 1;
todos.splice(todos.indexOf(todo), 1); todos.splice(todos.indexOf(todo), 1);
todoStorage.put(todos); todoStorage.put(todos);
}; };
$scope.todoCompleted = function (todo) { TC.todoCompleted = function (todo) {
$scope.remainingCount += todo.completed ? -1 : 1; TC.remainingCount += todo.completed ? -1 : 1;
todoStorage.put(todos); todoStorage.put(todos);
}; };
$scope.clearCompletedTodos = function () { TC.clearCompletedTodos = function () {
$scope.todos = todos = todos.filter(function (val) { TC.todos = todos = todos.filter(function (val) {
return !val.completed; return !val.completed;
}); });
todoStorage.put(todos); todoStorage.put(todos);
}; };
$scope.markAll = function (completed) { TC.markAll = function (completed) {
todos.forEach(function (todo) { todos.forEach(function (todo) {
todo.completed = completed; todo.completed = completed;
}); });
$scope.remainingCount = completed ? 0 : todos.length; TC.remainingCount = completed ? 0 : todos.length;
todoStorage.put(todos); 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