Commit 8c48866b authored by Pascal Hartig's avatar Pascal Hartig

firebase-angular: refactor controller

parent 20100b51
/*global todomvc, angular */ /*global todomvc, angular, Firebase */
'use strict'; 'use strict';
/** /**
...@@ -7,79 +7,82 @@ ...@@ -7,79 +7,82 @@
* - exposes the model to the template and provides event handlers * - exposes the model to the template and provides event handlers
*/ */
todomvc.controller('TodoCtrl', function TodoCtrl($scope, $location, angularFire) { todomvc.controller('TodoCtrl', function TodoCtrl($scope, $location, angularFire) {
var url = 'https://todomvc-angular.firebaseio.com/'; this.onDataLoaded = function onDataLoaded($scope, $location, url) {
$scope.$watch('todos', function () {
$scope.newTodo = ''; var total = 0;
$scope.editedTodo = null; var remaining = 0;
angular.forEach($scope.todos, function (todo) {
total++;
if (todo.completed === false) {
remaining++;
}
});
$scope.remainingCount = remaining;
$scope.completedCount = total - remaining;
$scope.allChecked = !$scope.remainingCount;
}, true);
if ($location.path() === '') { $scope.addTodo = function () {
$location.path('/'); var newTodo = $scope.newTodo.trim();
} if (!newTodo.length) {
$scope.location = $location; return;
}
$scope.todos[new Firebase(url).push().name()] = {
title: newTodo,
completed: false
};
$scope.newTodo = '';
};
angularFire(url, $scope, 'todos', {}).then(function() { $scope.editTodo = function (id) {
onDataLoaded($scope, $location, url); $scope.editedTodo = $scope.todos[id];
}); $scope.originalTodo = angular.extend({}, $scope.editedTodo);
}); };
function onDataLoaded($scope, $location, url) { $scope.doneEditing = function (id) {
$scope.$watch('todos', function () { $scope.editedTodo = null;
var total = 0; var title = $scope.todos[id].title.trim();
var remaining = 0; if (!title) {
angular.forEach($scope.todos, function(todo) { $scope.removeTodo(id);
total++; }
if (todo.completed === false) remaining++; };
});
$scope.remainingCount = remaining;
$scope.completedCount = total - remaining;
$scope.allChecked = !$scope.remainingCount;
}, true);
$scope.addTodo = function () { $scope.revertEditing = function (id) {
var newTodo = $scope.newTodo.trim(); $scope.todos[id] = $scope.originalTodo;
if (!newTodo.length) { $scope.doneEditing(id);
return;
}
$scope.todos[new Firebase(url).push().name()] = {
title: newTodo,
completed: false
}; };
$scope.newTodo = '';
};
$scope.editTodo = function (id) { $scope.removeTodo = function (id) {
$scope.editedTodo = $scope.todos[id]; delete $scope.todos[id];
$scope.originalTodo = angular.extend({}, $scope.editedTodo); };
};
$scope.doneEditing = function (id) { $scope.clearCompletedTodos = function () {
$scope.editedTodo = null; var notCompleted = {};
var title = $scope.todos[id].title.trim(); angular.forEach($scope.todos, function (todo, id) {
if (!title) { if (!todo.completed) {
$scope.removeTodo(id); notCompleted[id] = todo;
} }
}; });
$scope.todos = notCompleted;
};
$scope.revertEditing = function (id) { $scope.markAll = function (completed) {
$scope.todos[id] = $scope.originalTodo; angular.forEach($scope.todos, function (todo) {
$scope.doneEditing(id); todo.completed = completed;
});
};
}; };
$scope.removeTodo = function (id) { var url = 'https://todomvc-angular.firebaseio.com/';
delete $scope.todos[id]; $scope.newTodo = '';
}; $scope.editedTodo = null;
$scope.clearCompletedTodos = function () { if ($location.path() === '') {
var notCompleted = {}; $location.path('/');
angular.forEach($scope.todos, function (todo, id) { }
if (!todo.completed) notCompleted[id] = todo; $scope.location = $location;
});
$scope.todos = notCompleted;
};
$scope.markAll = function (completed) { angularFire(url, $scope, 'todos', {}).then(function () {
angular.forEach($scope.todos, function (todo) { this.onDataLoaded($scope, $location, url);
todo.completed = completed; }.bind(this));
}); });
};
}
\ No newline at end of file
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