re-imagined toderp, first commit.

This commit is contained in:
Max Lynch
2013-10-03 18:02:59 -05:00
parent a7fbaac5db
commit 0ecb2b58a8
6 changed files with 268 additions and 1 deletions

View File

@ -0,0 +1,4 @@
angular.module('ionic.todo', ['ionic.todo.services', 'ionic.todo.controllers', 'firebase', 'ngRoute', 'ngAnimate'])
// Our Firebase URL
.constant('FIREBASE_URL', 'https://ionic-todo-demo.firebaseio.com/');

View File

@ -0,0 +1,73 @@
angular.module('ionic.todo.controllers', ['ionic.todo', 'firebase'])
// The main controller for the application
.controller('TodoCtrl', function($scope, $rootScope, AuthService) {
$scope.display = {
screen: 'splash'
};
$rootScope.$on('angularFireAuth:login', function(evt, user) {
$scope.display.screen = 'tasks';
});
$rootScope.$on('angularFireAuth:logout', function(evt, user) {
console.log('Logged out!', evt, user);
$scope.display.screen = 'login';
});
$rootScope.$on('angularFireAuth:error', function(evt, err) {
console.log('Login Error!', evt, err);
});
$scope.setScreen = function(screen) {
$scope.display.screen = screen;
};
})
// The login form controller
.controller('LoginCtrl', function($scope, AuthService) {
console.log('Created login Ctrl');
$scope.loginForm = {
email: 'max@drifty.com',
password: 'test'
};
$scope.tryLogin = function(data) {
$scope.loginError = false;
AuthService.login(data.email, data.password)
.then(function(e) {
$scope.loginError = false;
}, function(e) {
$scope.loginError = true;
});
};
$scope.showSignup = function() {
$scope.setScreen('signup');
};
})
// The signup form controller
.controller('SignupCtrl', function($scope, AuthService) {
$scope.signupForm = {};
$scope.trySignup = function(data) {
AuthService.signup(data.email, data.password);
};
})
// The tasks controller (main app controller)
.controller('TasksCtrl', function($scope, angularFire, FIREBASE_URL) {
var taskRef = new Firebase(FIREBASE_URL + '/todos');
$scope.todos = [];
angularFire(taskRef, $scope, 'todos');
$scope.addTask = function(task) {
var t = {};
t = angular.extend({
id: $scope.user.id
}, task);
console.log("Adding task:", t);
$scope.todos.push(t);
$scope.task = {};
};
});

View File

@ -0,0 +1,46 @@
angular.module('ionic.todo.services', ['ionic.todo', 'firebase'])
// The AuthService handles user authentication with Firebase.
.factory('AuthService', function(angularFireAuth, $rootScope, FIREBASE_URL) {
// Initialize the angularFireAuth service.
var ref = new Firebase(FIREBASE_URL);
angularFireAuth.initialize(ref, {
scope: $rootScope,
callback: function(user, err) {
console.log('AUTH CHANGED', err, user);
},
name: 'user'
});
return {
// Try to log in with the given email and pass
login: function(email, password) {
if(!email || !password) {
return;
}
console.log('Logging in', email, password);
return angularFireAuth.login('password', {
email: email,
password: password
});
},
// Try to sign up with the given email and pass
signup: function(email, password) {
if(!email || !password) {
return;
}
console.log('Signing up', name, email, password);
angularFireAuth.createUser(email, password, function(err, user) {
if(err && err.code == "EMAIL_TAKEN") {
alert('That email is already taken');
}
});
}
};
})