Commit 5132f0e87c4dbda56df7c3cc9f9f0235bc539037
1 parent
c7a3eac3f4
Exists in
master
and in
1 other branch
clean up user service binding!
Showing 4 changed files with 43 additions and 59 deletions Side-by-side Diff
config.js
View file @
5132f0e
... | ... | @@ -125,11 +125,11 @@ |
125 | 125 | else return console.log('we are going to login anyway'); |
126 | 126 | } |
127 | 127 | if (UserService.isLoggedIn()) return console.log('no login required; going straight to ' + toState.name); |
128 | - | |
129 | - if (!UserService.isUserResolved()) return console.log('user not yet resolved; hold off'); | |
130 | 128 | $rootScope.returnToState = toState; |
131 | 129 | $rootScope.returnToStateParams = toStateParams; |
132 | 130 | console.log('going to ' + toState.name + ' after login'); |
131 | + event.preventDefault(); | |
132 | + $state.go('login'); | |
133 | 133 | |
134 | 134 | }); |
135 | 135 | $rootScope.$on('$stateChangeError', function(event, toState, toParams, fromState, fromParams, error) { |
home.html
View file @
5132f0e
... | ... | @@ -16,9 +16,9 @@ |
16 | 16 | <header> |
17 | 17 | <nav> |
18 | 18 | <div class="nav-wrapper"> |
19 | - <a href="#" data-activates="mobile-demo" class="left button-collapse hide-on-med-and-up"><i | |
19 | + <a ng-show="user" href="#" data-activates="mobile-demo" class="left button-collapse hide-on-med-and-up"><i | |
20 | 20 | class="mdi-navigation-menu"></i></a> |
21 | - <ul ng-show="currentSection.id && isLoggedIn" class="left hide-on-small-and-down"> | |
21 | + <ul ng-show="currentSection.id && UserService.isLoggedIn()" class="left hide-on-small-and-down"> | |
22 | 22 | <li ui-sref-active="active"><a ui-sref="feed({sectionId:currentSection.id})" class="tooltipped" |
23 | 23 | data-position="bottom" |
24 | 24 | data-delay="50" data-tooltip="Feed"><i |
25 | 25 | |
... | ... | @@ -34,10 +34,10 @@ |
34 | 34 | </ul> |
35 | 35 | <a href="#" class="brand-logo center">Flashy</a> |
36 | 36 | |
37 | - <ul ng-show="isLoggedIn" id="nav-mobile" class="right hide-on-small-and-down"> | |
37 | + <ul ng-show="UserService.isLoggedIn()" id="nav-mobile" class="right hide-on-small-and-down"> | |
38 | 38 | <!-- User's classes dropdown --> |
39 | 39 | <ul id="dropdown1" class="dropdown-content"> |
40 | - <li ui-sref-active="active" ng-repeat="section in sections"> | |
40 | + <li ui-sref-active="active" ng-repeat="section in UserService.getUserData().sections"> | |
41 | 41 | <a class="class bold" ui-sref="feed({sectionId:section.id})">{{section.short_name}}</a> |
42 | 42 | </li> |
43 | 43 | <li class="divider"></li> |
... | ... | @@ -50,20 +50,18 @@ |
50 | 50 | </ul> |
51 | 51 | |
52 | 52 | <!-- Slide-in side-nav for small screens --> |
53 | - <ul class="side-nav" id="mobile-demo"> | |
54 | - <ul ng-show="currentSection.id && isLoggedIn"> | |
55 | - <li ui-sref-active="active"><a ui-sref="feed({sectionId:currentSection.id})" class="tooltipped" | |
56 | - ><i | |
57 | - class="mdi-action-view-module left"></i>Feed</a></li> | |
58 | - <li ui-sref-active="active"><a ui-sref="deck({sectionId:currentSection.id})" class="tooltipped" | |
59 | - ><i | |
60 | - class="mdi-action-view-carousel left"></i>Deck</a></li> | |
61 | - <li ui-sref-active="active"><a ui-sref="cardlist({sectionId:currentSection.id})" class="tooltipped" | |
62 | - ><i | |
63 | - class="mdi-action-view-list left"></i>Card List</a> | |
64 | - </li> | |
65 | - <hr> | |
66 | - </ul> | |
53 | + <ul ng-show="currentSection.id && UserService.isLoggedIn()" class="side-nav" id="mobile-demo"> | |
54 | + <li ui-sref-active="active"><a ui-sref="feed({sectionId:currentSection.id})" class="tooltipped" | |
55 | + ><i | |
56 | + class="mdi-action-view-module left"></i>Feed</a></li> | |
57 | + <li ui-sref-active="active"><a ui-sref="deck({sectionId:currentSection.id})" class="tooltipped" | |
58 | + ><i | |
59 | + class="mdi-action-view-carousel left"></i>Deck</a></li> | |
60 | + <li ui-sref-active="active"><a ui-sref="cardlist({sectionId:currentSection.id})" class="tooltipped" | |
61 | + ><i | |
62 | + class="mdi-action-view-list left"></i>Card List</a> | |
63 | + </li> | |
64 | + <hr> | |
67 | 65 | |
68 | 66 | <!-- Collapsible menu for all the User's classes --> |
69 | 67 | <ul class="collapsible" data-collapsible="accordion"> |
scripts/RootController.js
View file @
5132f0e
... | ... | @@ -3,12 +3,16 @@ |
3 | 3 | controller('RootController', function($rootScope, $resource, $scope, $state, UserService) { |
4 | 4 | $rootScope.SectionResource = $resource('/api/sections/:sectionId/'); |
5 | 5 | window.rootscope = $rootScope; |
6 | - $rootScope.isLoggedIn = false; | |
6 | + window.userservice = UserService; | |
7 | 7 | $rootScope.currentSection = {}; |
8 | + $scope.UserService = UserService; | |
8 | 9 | UserService.getUserData().then(function(data) { |
9 | - console.log(data); | |
10 | - $rootScope.user = data; | |
10 | + $scope.user = data; | |
11 | 11 | }); |
12 | + //UserService.getUserData().then(function(data) { | |
13 | + // console.log(data); | |
14 | + // $rootScope.user = data; | |
15 | + //}); | |
12 | 16 | $('.button-collapse').sideNav({ |
13 | 17 | menuWidth: 240, // Default is 240 |
14 | 18 | edge: 'left', // Choose the horizontal origin |
scripts/UserService.js
View file @
5132f0e
1 | 1 | angular.module('flashy.UserService', ['ui.router']). |
2 | 2 | service('UserService', function($rootScope, $http, $q) { |
3 | - var _user = undefined; | |
4 | - $rootScope.sections = []; | |
5 | - this.getUserData = function(refresh) { | |
6 | - var deferred = $q.defer(); | |
7 | - if (angular.isDefined(_user)) { | |
8 | - console.log('user is already defined: ' + _user); | |
9 | - if (!_user) { | |
10 | - console.log('rejecting deferred'); | |
11 | - deferred.reject(_user); | |
12 | - } else { | |
13 | - $rootScope.isLoggedIn = true; | |
14 | - $rootScope.sections = _user.sections; | |
15 | - deferred.resolve(_user); | |
16 | - } | |
17 | - return deferred.promise; | |
18 | - } | |
19 | - $http.get('/api/me/').success(function(data) { | |
20 | - console.log('user is logged in!'); | |
21 | - _user = data; | |
22 | - $rootScope.isLoggedIn = true; | |
23 | - $rootScope.sections = _user.sections; | |
24 | - deferred.resolve(data); | |
25 | - }).error(function(data) { | |
26 | - console.log(data); | |
27 | - console.log('not logged in yet: ' + data.detail); | |
28 | - deferred.reject('error getting own data'); | |
29 | - _user = false; | |
30 | - }); | |
31 | - return deferred.promise; | |
3 | + var deferred = $q.defer(); | |
4 | + | |
5 | + $http.get('/api/me/').success(function(data) { | |
6 | + console.log('user is logged in!'); | |
7 | + _user = data; | |
8 | + deferred.resolve(data); | |
9 | + }).error(function(data) { | |
10 | + console.log(data); | |
11 | + console.log('not logged in yet: ' + data.detail); | |
12 | + deferred.resolve({}); | |
13 | + }); | |
14 | + | |
15 | + var _user = deferred.promise; | |
16 | + | |
17 | + this.getUserData = function() { | |
18 | + return _user; | |
32 | 19 | }; |
33 | 20 | this.setUserData = function(data) { |
34 | 21 | _user = data; |
35 | 22 | }; |
36 | - this.isUserResolved = function() { | |
37 | - return angular.isDefined(_user); | |
38 | - }; | |
39 | 23 | this.logout = function() { |
40 | - $rootScope.isLoggedIn = false; | |
41 | - _user = undefined; | |
24 | + _user = {}; | |
42 | 25 | }; |
43 | 26 | this.isLoggedIn = function() { |
44 | - rv = this.isUserResolved() && _user; | |
45 | - console.log('is user logged in? ' + rv); | |
27 | + rv = angular.isDefined(_user.email); | |
46 | 28 | return rv; |
47 | 29 | }; |
48 | 30 | }); |