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,28 +7,15 @@ ...@@ -7,28 +7,15 @@
* - 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.newTodo = '';
$scope.editedTodo = null;
if ($location.path() === '') {
$location.path('/');
}
$scope.location = $location;
angularFire(url, $scope, 'todos', {}).then(function() {
onDataLoaded($scope, $location, url);
});
});
function onDataLoaded($scope, $location, url) {
$scope.$watch('todos', function () { $scope.$watch('todos', function () {
var total = 0; var total = 0;
var remaining = 0; var remaining = 0;
angular.forEach($scope.todos, function(todo) { angular.forEach($scope.todos, function (todo) {
total++; total++;
if (todo.completed === false) remaining++; if (todo.completed === false) {
remaining++;
}
}); });
$scope.remainingCount = remaining; $scope.remainingCount = remaining;
$scope.completedCount = total - remaining; $scope.completedCount = total - remaining;
...@@ -72,7 +59,9 @@ function onDataLoaded($scope, $location, url) { ...@@ -72,7 +59,9 @@ function onDataLoaded($scope, $location, url) {
$scope.clearCompletedTodos = function () { $scope.clearCompletedTodos = function () {
var notCompleted = {}; var notCompleted = {};
angular.forEach($scope.todos, function (todo, id) { angular.forEach($scope.todos, function (todo, id) {
if (!todo.completed) notCompleted[id] = todo; if (!todo.completed) {
notCompleted[id] = todo;
}
}); });
$scope.todos = notCompleted; $scope.todos = notCompleted;
}; };
...@@ -82,4 +71,18 @@ function onDataLoaded($scope, $location, url) { ...@@ -82,4 +71,18 @@ function onDataLoaded($scope, $location, url) {
todo.completed = completed; todo.completed = completed;
}); });
}; };
} };
\ No newline at end of file
var url = 'https://todomvc-angular.firebaseio.com/';
$scope.newTodo = '';
$scope.editedTodo = null;
if ($location.path() === '') {
$location.path('/');
}
$scope.location = $location;
angularFire(url, $scope, 'todos', {}).then(function () {
this.onDataLoaded($scope, $location, url);
}.bind(this));
});
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