Skip to content

Commit

Permalink
Update routes to newRouter syntax and fix much (but not all) of the b…
Browse files Browse the repository at this point in the history
…reakage. Note that the use of $rootScope is a hack (maybe?) to deal with angular/router#192.
  • Loading branch information
ggranum committed Mar 18, 2015
1 parent c98bfcd commit c562010
Show file tree
Hide file tree
Showing 18 changed files with 392 additions and 335 deletions.
12 changes: 11 additions & 1 deletion .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

69 changes: 31 additions & 38 deletions app/components/_app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

// Declare app level module which depends on filters, and services
var myApp = angular.module('myApp', [
'ngNewRouter',
'ngAria',
'ngMaterial',
'myApp.routes',
'myApp.config',
'myApp.home',
'myApp.leftVerticalBar',
Expand All @@ -18,22 +18,22 @@
'myApp.chat'
]);

myApp.config(["$mdThemingProvider", function ($mdThemingProvider) {

$mdThemingProvider.theme('default')
.primaryPalette('indigo')
.accentPalette('pink');
}]);
myApp.config([
"$mdThemingProvider", function ($mdThemingProvider) {
$mdThemingProvider.theme('default')
.primaryPalette('indigo')
.accentPalette('pink');
}]);

myApp.controller('AppController',
["$scope", "$timeout", "$mdSidenav", "$log", function ($scope, $timeout, $mdSidenav, $log) {
$scope.toggleRight = function () {
$mdSidenav('right').toggle()
.then(function (foo) {
$log.debug("toggle RIGHT is done");
});
};
}]);
myApp.controller('AppController', [
"$mdSidenav", "$log", function ($mdSidenav, $log) {
var app = this;
app.toggleRight = function () {
$mdSidenav('right').toggle().then(function () {
$log.debug("toggle RIGHT is done");
});
};
}]);

var core = angular.module('myApp.core', []);

Expand All @@ -51,29 +51,23 @@

core.factory("userFactory", [
'$q', 'simpleLogin', '$firebaseObject', 'FBURL', function ($q, simpleLogin, $firebaseObject, FBURL) {
function current(){
var userPromise = simpleLogin.getUser();
return userPromise.then(function (authUser) {
var profile;
if (authUser) {
// equivalent to calling (assuming you inject all the extra parameters):
// "profile = $firebase(new $window.Firebase([FBURL, 'users', authUser.uid].join('/'))).$asObject();"
profile = $firebaseObject(new Firebase(FBURL + '/users/' + authUser.uid)).$loaded();
} else {
profile = $q.when(null);
var self = this;
angular.extend(self,
{
current: function () {
return simpleLogin.getUser().then(function (authUser) {
if (authUser) {
return self.getUserProfile(authUser.uid);
} else {
return $q.when(null);
}
});
},
getUserProfile: function (uid) {
return $firebaseObject(new Firebase(FBURL + '/users/' + uid)).$loaded();
}
return profile;
});
}
function getUser(uid){
return $firebaseObject(new Firebase(FBURL + '/users/' + uid)).$loaded();
}


return {
current: current,
getUser: getUser
};
return self;
}]);

core.filter('interpolate', [
Expand All @@ -83,5 +77,4 @@
};
}]);


}());
184 changes: 98 additions & 86 deletions app/components/_app/routes.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
(function () {
"use strict";

angular.module('myApp.routes', ['ngRoute', 'simpleLogin'])
var module = angular.module('myApp');

.constant('ROUTES', {


module.controller('AppController', ['$router', function($router){
$router.config([
{path: '/', redirectTo: '/home'},
{path: '/home', component: 'home'},
{path: '/chat', component: 'chat'},
{path: '/login', component: 'login'},
{path: '/account', component: 'account'}
]);
}]);

module.constant('ROUTES', {
'/home': {
templateUrl: 'components/home/home.html',
controller: 'HomeCtrl',
controller: 'HomeController',
resolve: {
// forces the page to wait for this promise to resolve before controller is loaded
// the controller can then inject `user` as a dependency. This could also be done
Expand All @@ -20,99 +32,99 @@
},
'/chat': {
templateUrl: 'components/chat/chat.html',
controller: 'ChatCtrl'
controller: 'ChatController'
},
'/login': {
templateUrl: 'components/login/login.html',
controller: 'LoginCtrl'
controller: 'LoginController'
},
'/account': {
templateUrl: 'components/account/account.html',
controller: 'AccountCtrl',
controller: 'AccountController',
// require user to be logged in to view this route
// the whenAuthenticated method below will resolve the current user
// before this controller loads and redirect if necessary
authRequired: true
}
})

/**
* Adds a special `whenAuthenticated` method onto $routeProvider. This special method,
* when called, invokes the requireUser() service (see simpleLogin.js).
*
* The promise either resolves to the authenticated user object and makes it available to
* dependency injection (see AuthCtrl), or rejects the promise if user is not logged in,
* forcing a redirect to the /login page
*/
.config([
'$routeProvider', function ($routeProvider) {
// credits for this idea: https://groups.google.com/forum/#!msg/angular/dPr9BpIZID0/MgWVluo_Tg8J
// unfortunately, a decorator cannot be use here because they are not applied until after
// the .config calls resolve, so they can't be used during route configuration, so we have
// to hack it directly onto the $routeProvider object
$routeProvider.whenAuthenticated = function (path, route) {
route.resolve = route.resolve || {};
route.resolve.user = [
'requireUser', function (requireUser) {
return requireUser();
}];
$routeProvider.when(path, route);
};
}])

// configure views; the authRequired parameter is used for specifying pages
// which should only be available while logged in
.config([
'$routeProvider', 'ROUTES', function ($routeProvider, ROUTES) {
angular.forEach(ROUTES, function (route, path) {
if (route.authRequired) {
// adds a {resolve: user: {...}} promise which is rejected if
// the user is not authenticated or fulfills with the user object
// on success (the user object is then available to dependency injection)
$routeProvider.whenAuthenticated(path, route);
}
else {
// all other routes are added normally
$routeProvider.when(path, route);
}
});
// routes which are not in our map are redirected to /home
$routeProvider.otherwise({redirectTo: '/home'});
}])

/**
* Apply some route security. Any route's resolve method can reject the promise with
* { authRequired: true } to force a redirect. This method enforces that and also watches
* for changes in auth status which might require us to navigate away from a path
* that we can no longer view.
*/
.run([
'$rootScope', '$location', 'simpleLogin', 'ROUTES', 'loginRedirectPath',
function ($rootScope, $location, simpleLogin, ROUTES, loginRedirectPath) {
// watch for login status changes and redirect if appropriate
simpleLogin.watch(check, $rootScope);
});

// some of our routes may reject resolve promises with the special {authRequired: true} error
// this redirects to the login page whenever that is encountered
$rootScope.$on("$routeChangeError", function (e, next, prev, err) {
if (angular.isObject(err) && err.authRequired) {
$location.path(loginRedirectPath);
}
});

function check(user) {
// used by the changeEmail functionality so the user
// isn't redirected to the login screen while we switch
// out the accounts (see changeEmail.js)
if ($rootScope.authChangeInProgress) { return; }
if (!user && authRequired($location.path())) {
$location.path(loginRedirectPath);
}
}

function authRequired(path) {
return ROUTES.hasOwnProperty(path) && ROUTES[path].authRequired;
}
}
]);
///**
// * Adds a special `whenAuthenticated` method onto $routeProvider. This special method,
// * when called, invokes the requireUser() service (see simpleLogin.js).
// *
// * The promise either resolves to the authenticated user object and makes it available to
// * dependency injection (see AuthCtrl), or rejects the promise if user is not logged in,
// * forcing a redirect to the /login page
// */
//module.config([
// '$routeProvider', function ($routeProvider) {
// // credits for this idea: https://groups.google.com/forum/#!msg/angular/dPr9BpIZID0/MgWVluo_Tg8J
// // unfortunately, a decorator cannot be use here because they are not applied until after
// // the .config calls resolve, so they can't be used during route configuration, so we have
// // to hack it directly onto the $routeProvider object
// $routeProvider.whenAuthenticated = function (path, route) {
// route.resolve = route.resolve || {};
// route.resolve.user = [
// 'requireUser', function (requireUser) {
// return requireUser();
// }];
// $routeProvider.when(path, route);
// };
// }])
//
// // configure views; the authRequired parameter is used for specifying pages
// // which should only be available while logged in
// .config([
// '$routeProvider', 'ROUTES', function ($routeProvider, ROUTES) {
// angular.forEach(ROUTES, function (route, path) {
// if (route.authRequired) {
// // adds a {resolve: user: {...}} promise which is rejected if
// // the user is not authenticated or fulfills with the user object
// // on success (the user object is then available to dependency injection)
// $routeProvider.whenAuthenticated(path, route);
// }
// else {
// // all other routes are added normally
// $routeProvider.when(path, route);
// }
// });
// // routes which are not in our map are redirected to /home
// $routeProvider.otherwise({redirectTo: '/home'});
// }]);
//
///**
// * Apply some route security. Any route's resolve method can reject the promise with
// * { authRequired: true } to force a redirect. This method enforces that and also watches
// * for changes in auth status which might require us to navigate away from a path
// * that we can no longer view.
// */
//module.run([
// '$rootScope', '$location', 'simpleLogin', 'ROUTES', 'loginRedirectPath',
// function ($rootScope, $location, simpleLogin, ROUTES, loginRedirectPath) {
// // watch for login status changes and redirect if appropriate
// simpleLogin.watch(check, $rootScope);
//
// // some of our routes may reject resolve promises with the special {authRequired: true} error
// // this redirects to the login page whenever that is encountered
// $rootScope.$on("$routeChangeError", function (e, next, prev, err) {
// if (angular.isObject(err) && err.authRequired) {
// $location.path(loginRedirectPath);
// }
// });
//
// function check(user) {
// // used by the changeEmail functionality so the user
// // isn't redirected to the login screen while we switch
// // out the accounts (see changeEmail.js)
// if ($rootScope.authChangeInProgress) { return; }
// if (!user && authRequired($location.path())) {
// $location.path(loginRedirectPath);
// }
// }
//
// function authRequired(path) {
// return ROUTES.hasOwnProperty(path) && ROUTES[path].authRequired;
// }
// }
// ]);
}());
Loading

0 comments on commit c562010

Please sign in to comment.