Skip to content

Commit 6147e76

Browse files
committed
refactor(client:auth): use named function, create Auth reference
1 parent cb0e4ab commit 6147e76

File tree

2 files changed

+47
-38
lines changed

2 files changed

+47
-38
lines changed
Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
'use strict';
2+
23
(function() {
34

4-
function MainController($scope, $http<% if (filters.socketio) { %>, socket<% } %>) {
5-
var self = this;
6-
this.awesomeThings = [];
7-
8-
$http.get('/api/things').then(function(response) {
9-
self.awesomeThings = response.data;<% if (filters.socketio) { %>
10-
socket.syncUpdates('thing', self.awesomeThings);<% } %>
11-
});
12-
<% if (filters.models) { %>
13-
this.addThing = function() {
14-
if (self.newThing === '') {
15-
return;
16-
}
17-
$http.post('/api/things', { name: self.newThing });
18-
self.newThing = '';
19-
};
20-
21-
this.deleteThing = function(thing) {
22-
$http.delete('/api/things/' + thing._id);
23-
};<% } %><% if (filters.socketio) { %>
24-
25-
$scope.$on('$destroy', function() {
26-
socket.unsyncUpdates('thing');
27-
});<% } %>
28-
}
29-
30-
angular.module('<%= scriptAppName %>')
31-
.controller('MainController', MainController);
5+
function MainController($scope, $http<% if (filters.socketio) { %>, socket<% } %>) {
6+
var self = this;
7+
this.awesomeThings = [];
8+
9+
$http.get('/api/things').then(function(response) {
10+
self.awesomeThings = response.data;<% if (filters.socketio) { %>
11+
socket.syncUpdates('thing', self.awesomeThings);<% } %>
12+
});<% if (filters.models) { %>
13+
14+
this.addThing = function() {
15+
if (self.newThing === '') {
16+
return;
17+
}
18+
$http.post('/api/things', { name: self.newThing });
19+
self.newThing = '';
20+
};
21+
22+
this.deleteThing = function(thing) {
23+
$http.delete('/api/things/' + thing._id);
24+
};<% } if (filters.socketio) { %>
25+
26+
$scope.$on('$destroy', function() {
27+
socket.unsyncUpdates('thing');
28+
});<% } %>
29+
}
30+
31+
angular.module('<%= scriptAppName %>')
32+
.controller('MainController', MainController);
3233

3334
})();

app/templates/client/components/auth(auth)/auth.service(js).js

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

3-
angular.module('<%= scriptAppName %>')
4-
.factory('Auth', function Auth($http, User, $cookies, $q) {
3+
(function() {
4+
5+
function AuthService($http, User, $cookies, $q) {
56
/**
67
* Return a callback or noop function
78
*
@@ -18,7 +19,7 @@ angular.module('<%= scriptAppName %>')
1819
currentUser = User.get();
1920
}
2021

21-
return {
22+
var Auth = {
2223

2324
/**
2425
* Authenticate user and save token
@@ -42,10 +43,10 @@ angular.module('<%= scriptAppName %>')
4243
return user;
4344
})
4445
.catch(function(err) {
45-
this.logout();
46+
Auth.logout();
4647
safeCb(callback)(err.data);
4748
return $q.reject(err.data);
48-
}.bind(this));
49+
});
4950
},
5051

5152
/**
@@ -71,9 +72,9 @@ angular.module('<%= scriptAppName %>')
7172
return safeCb(callback)(null, user);
7273
},
7374
function(err) {
74-
this.logout();
75+
Auth.logout();
7576
return safeCb(callback)(err);
76-
}.bind(this)).$promise;
77+
}).$promise;
7778
},
7879

7980
/**
@@ -130,7 +131,7 @@ angular.module('<%= scriptAppName %>')
130131
return currentUser.hasOwnProperty('role');
131132
}
132133

133-
return this.getCurrentUser(null)
134+
return Auth.getCurrentUser(null)
134135
.then(function(user) {
135136
var is = user.hasOwnProperty('role');
136137
safeCb(callback)(is);
@@ -150,7 +151,7 @@ angular.module('<%= scriptAppName %>')
150151
return currentUser.role === 'admin';
151152
}
152153

153-
return this.getCurrentUser(null)
154+
return Auth.getCurrentUser(null)
154155
.then(function(user) {
155156
var is = user.role === 'admin';
156157
safeCb(callback)(is);
@@ -167,4 +168,11 @@ angular.module('<%= scriptAppName %>')
167168
return $cookies.get('token');
168169
}
169170
};
170-
});
171+
172+
return Auth;
173+
}
174+
175+
angular.module('<%= scriptAppName %>')
176+
.factory('Auth', AuthService);
177+
178+
})();

0 commit comments

Comments
 (0)