Skip to content

Commit 6aadee6

Browse files
kingcodyDaftMonk
authored andcommitted
feat(app-routing): improve app routing
Changes: - Use `ui-sref` instead of `href` or `ng-href` when ui-router is chosen - Use `ui-sref-active` instead of `ng-class='{active: isActive()}'` when ui-router is chosen - Use `$state.go()` where applicable, when ui-router is chosen - Use `$scope.menu[n].state` instead of `$scope.menu[n].link` when ui-router is chosen (attempt to remove possible confusion) - Omit `$scope.isActive` when ui-router is chosen - Simplify `navbar(jade).jade` templating (remove extra `<% if (filters.auth) %>` tag) - Add `/logout` route for both ng-route and ui-router - Use `$routeChangeStart` or `$stateChangeStart` to pass refering route or state to `/logout` - Add `stateMock` for testing `$state` transitions - Use `stateMock` in main.controller for expecting template requests from state transistions Closes #331
1 parent 2aaff12 commit 6aadee6

20 files changed

+191
-72
lines changed

app/templates/client/app/account(auth)/account(coffee).coffee

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ angular.module '<%= scriptAppName %>'
77
templateUrl: 'app/account/login/login.html'
88
controller: 'LoginCtrl'
99

10+
.when '/logout',
11+
name: 'logout'
12+
referrer: '/'
13+
controller: ($location, $route, Auth) ->
14+
referrer = $route.current.params.referrer or $route.current.referrer or "/"
15+
Auth.logout()
16+
$location.path referrer
17+
1018
.when '/signup',
1119
templateUrl: 'app/account/signup/signup.html'
1220
controller: 'SignupCtrl'
@@ -15,13 +23,25 @@ angular.module '<%= scriptAppName %>'
1523
templateUrl: 'app/account/settings/settings.html'
1624
controller: 'SettingsCtrl'
1725
authenticate: true
26+
27+
.run ($rootScope) ->
28+
$rootScope.$on '$routeChangeStart', (event, next, current) ->
29+
next.referrer = current.originalPath if next.name is "logout" and current and current.originalPath and not current.authenticate
1830
<% } %><% if(filters.uirouter) { %>.config ($stateProvider) ->
1931
$stateProvider
2032
.state 'login',
2133
url: '/login'
2234
templateUrl: 'app/account/login/login.html'
2335
controller: 'LoginCtrl'
2436

37+
.state 'logout',
38+
url: '/logout?referrer'
39+
referrer: 'main'
40+
controller: ($state, Auth) ->
41+
referrer = $state.params.referrer or $state.current.referrer or "main"
42+
Auth.logout()
43+
$state.go referrer
44+
2545
.state 'signup',
2646
url: '/signup'
2747
templateUrl: 'app/account/signup/signup.html'
@@ -32,4 +52,8 @@ angular.module '<%= scriptAppName %>'
3252
templateUrl: 'app/account/settings/settings.html'
3353
controller: 'SettingsCtrl'
3454
authenticate: true
35-
<% } %>
55+
56+
.run ($rootScope) ->
57+
$rootScope.$on '$stateChangeStart', (event, next, nextParams, current) ->
58+
next.referrer = current.name if next.name is "logout" and current and current.name and not current.authenticate
59+
<% } %>

app/templates/client/app/account(auth)/account(js).js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ angular.module('<%= scriptAppName %>')
77
templateUrl: 'app/account/login/login.html',
88
controller: 'LoginCtrl'
99
})
10+
.when('/logout', {
11+
name: 'logout',
12+
referrer: '/',
13+
controller: function($location, $route, Auth) {
14+
var referrer = $route.current.params.referrer ||
15+
$route.current.referrer ||
16+
'/';
17+
Auth.logout();
18+
$location.path(referrer);
19+
}
20+
})
1021
.when('/signup', {
1122
templateUrl: 'app/account/signup/signup.html',
1223
controller: 'SignupCtrl'
@@ -16,13 +27,31 @@ angular.module('<%= scriptAppName %>')
1627
controller: 'SettingsCtrl',
1728
authenticate: true
1829
});
30+
})
31+
.run(function($rootScope) {
32+
$rootScope.$on('$routeChangeStart', function(event, next, current) {
33+
if (next.name === 'logout' && current && current.originalPath && !current.authenticate) {
34+
next.referrer = current.originalPath;
35+
}
36+
});
1937
});<% } %><% if(filters.uirouter) { %>.config(function ($stateProvider) {
2038
$stateProvider
2139
.state('login', {
2240
url: '/login',
2341
templateUrl: 'app/account/login/login.html',
2442
controller: 'LoginCtrl'
2543
})
44+
.state('logout', {
45+
url: '/logout?referrer',
46+
referrer: 'main',
47+
controller: function($state, Auth) {
48+
var referrer = $state.params.referrer ||
49+
$state.current.referrer ||
50+
'main';
51+
Auth.logout();
52+
$state.go(referrer);
53+
}
54+
})
2655
.state('signup', {
2756
url: '/signup',
2857
templateUrl: 'app/account/signup/signup.html',
@@ -34,4 +63,11 @@ angular.module('<%= scriptAppName %>')
3463
controller: 'SettingsCtrl',
3564
authenticate: true
3665
});
37-
});<% } %>
66+
})
67+
.run(function($rootScope) {
68+
$rootScope.$on('$stateChangeStart', function(event, next, nextParams, current) {
69+
if (next.name === 'logout' && current && current.name && !current.authenticate) {
70+
next.referrer = current.name;
71+
}
72+
});
73+
});<% } %>

app/templates/client/app/account(auth)/login/login(html).html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ <h1>Login</h1>
3737
<button class="btn btn-inverse btn-lg btn-login" type="submit">
3838
Login
3939
</button>
40-
<a class="btn btn-default btn-lg btn-register" href="/signup">
40+
<a class="btn btn-default btn-lg btn-register" <% if(filters.uirouter) { %>ui-sref="signup"<% } else { %>href="/signup"<% } %>>
4141
Register
4242
</a>
4343
</div>

app/templates/client/app/account(auth)/login/login(jade).jade

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ div(ng-include='"components/navbar/navbar.html"')
3434
button.btn.btn-inverse.btn-lg.btn-login(type='submit')
3535
| Login
3636
= ' '
37-
a.btn.btn-default.btn-lg.btn-register(href='/signup')
37+
a.btn.btn-default.btn-lg.btn-register(<% if(filters.uirouter) { %>ui-sref='signup'<% } else { %>href='/signup'<% } %>)
3838
| Register
3939
<% if(filters.oauth) {%>
4040
hr

app/templates/client/app/account(auth)/login/login.controller(coffee).coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
angular.module '<%= scriptAppName %>'
4-
.controller 'LoginCtrl', ($scope, Auth, $location<% if(filters.oauth) {%>, $window<% } %>) ->
4+
.controller 'LoginCtrl', ($scope, Auth<% if(filters.ngroute) { %>, $location<% } %><% if(filters.uirouter) { %>, $state<% } %><% if(filters.oauth) {%>, $window<% } %>) ->
55
$scope.user = {}
66
$scope.errors = {}
77
$scope.login = (form) ->
@@ -14,7 +14,7 @@ angular.module '<%= scriptAppName %>'
1414
password: $scope.user.password
1515

1616
.then ->
17-
$location.path '/'
17+
<% if(filters.ngroute) { %>$location.path '/'<% } %><% if(filters.uirouter) { %>$state.go 'main'<% } %>
1818

1919
.catch (err) ->
2020
$scope.errors.other = err.message

app/templates/client/app/account(auth)/login/login.controller(js).js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
angular.module('<%= scriptAppName %>')
4-
.controller('LoginCtrl', function ($scope, Auth, $location<% if (filters.oauth) { %>, $window<% } %>) {
4+
.controller('LoginCtrl', function ($scope, Auth<% if(filters.ngroute) { %>, $location<% } %><% if(filters.uirouter) { %>, $state<% } %><% if (filters.oauth) { %>, $window<% } %>) {
55
$scope.user = {};
66
$scope.errors = {};
77

@@ -15,7 +15,7 @@ angular.module('<%= scriptAppName %>')
1515
})
1616
.then( function() {
1717
// Logged in, redirect to home
18-
$location.path('/');
18+
<% if(filters.ngroute) { %>$location.path('/');<% } %><% if(filters.uirouter) { %>$state.go('main');<% } %>
1919
})
2020
.catch( function(err) {
2121
$scope.errors.other = err.message;

app/templates/client/app/account(auth)/signup/signup(html).html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ <h1>Sign up</h1>
5858
<button class="btn btn-inverse btn-lg btn-login" type="submit">
5959
Sign up
6060
</button>
61-
<a class="btn btn-default btn-lg btn-register" href="/login">
61+
<a class="btn btn-default btn-lg btn-register" <% if(filters.uirouter) { %>ui-sref="login"<% } else { %>href="/login"<% } %>>
6262
Login
6363
</a>
6464
</div>

app/templates/client/app/account(auth)/signup/signup(jade).jade

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ div(ng-include='"components/navbar/navbar.html"')
3636
button.btn.btn-inverse.btn-lg.btn-login(type='submit')
3737
| Sign up
3838
= ' '
39-
a.btn.btn-default.btn-lg.btn-register(href='/login')
39+
a.btn.btn-default.btn-lg.btn-register(<% if(filters.uirouter) { %>ui-sref='login'<% } else { %>href='/login'<% } %>)
4040
| Login
4141

4242
<% if(filters.oauth) {%>

app/templates/client/app/account(auth)/signup/signup.controller(coffee).coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
angular.module '<%= scriptAppName %>'
4-
.controller 'SignupCtrl', ($scope, Auth, $location<% if(filters.oauth) {%>, $window<% } %>) ->
4+
.controller 'SignupCtrl', ($scope, Auth<% if(filters.ngroute) { %>, $location<% } %><% if(filters.uirouter) { %>, $state<% } %><% if(filters.oauth) {%>, $window<% } %>) ->
55
$scope.user = {}
66
$scope.errors = {}
77
$scope.register = (form) ->
@@ -15,7 +15,7 @@ angular.module '<%= scriptAppName %>'
1515
password: $scope.user.password
1616

1717
.then ->
18-
$location.path '/'
18+
<% if(filters.ngroute) { %>$location.path '/'<% } %><% if(filters.uirouter) { %>$state.go 'main'<% } %>
1919

2020
.catch (err) ->
2121
err = err.data

app/templates/client/app/account(auth)/signup/signup.controller(js).js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
angular.module('<%= scriptAppName %>')
4-
.controller('SignupCtrl', function ($scope, Auth, $location<% if (filters.oauth) { %>, $window<% } %>) {
4+
.controller('SignupCtrl', function ($scope, Auth<% if(filters.ngroute) { %>, $location<% } %><% if(filters.uirouter) { %>, $state<% } %><% if (filters.oauth) { %>, $window<% } %>) {
55
$scope.user = {};
66
$scope.errors = {};
77

@@ -16,7 +16,7 @@ angular.module('<%= scriptAppName %>')
1616
})
1717
.then( function() {
1818
// Account created, redirect to home
19-
$location.path('/');
19+
<% if(filters.ngroute) { %>$location.path('/');<% } %><% if(filters.uirouter) { %>$state.go('main');<% } %>
2020
})
2121
.catch( function(err) {
2222
err = err.data;

0 commit comments

Comments
 (0)