var todoApp = angular.module('TodoApp', ['firebase']);
todoApp.controller('TodoCtrl', ['$scope', '$firebaseArray',
function($scope, $firebaseArray) {
// CREATE A FIREBASE REFERENCE
var todosRef = new Firebase('https://teamtododemo.firebaseio.com/');
// GET TODOS AS AN ARRAY
$scope.todos = $firebaseArray(todosRef);
// ADD TODO ITEM METHOD
$scope.addTodo = function () {
// CREATE A UNIQUE ID
var timestamp = new Date().valueOf();
$scope.todos.$add({
id: timestamp,
name: $scope.todoName,
status: 'pending'
});
$scope.todoName = "";
};
// REMOVE TODO ITEM METHOD
$scope.removeTodo = function (index, todo) {
// CHECK THAT ITEM IS VALID
if (todo.id === undefined)return;
// FIREBASE: REMOVE ITEM FROM LIST
$scope.todos.$remove(todo);
};
// MARK TODO AS IN PROGRESS METHOD
$scope.startTodo = function (index, todo) {
// CHECK THAT ITEM IS VALID
if (todo.id === undefined)return;
// UPDATE STATUS TO IN PROGRESS AND SAVE
todo.status = 'in progress';
$scope.todos.$save(todo);
};
// MARK TODO AS COMPLETE METHOD
$scope.completeTodo = function (index, todo) {
// CHECK THAT ITEM IS VALID
if (todo.id === undefined)return;
// UPDATE STATUS TO COMPLETE AND SAVE
todo.status = 'complete';
$scope.todos.$save(todo);
};
}]);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.