From 14354a06d8d14d1234252162471d6a2a486bac1c Mon Sep 17 00:00:00 2001 From: kingcody Date: Mon, 14 Sep 2015 03:49:01 -0400 Subject: [PATCH] refactor(client:auth): use named function, create Auth reference --- .../client/app/main/main.controller(js).js | 57 ++++++++++--------- .../components/auth(auth)/auth.service(js).js | 28 +++++---- 2 files changed, 47 insertions(+), 38 deletions(-) diff --git a/app/templates/client/app/main/main.controller(js).js b/app/templates/client/app/main/main.controller(js).js index 35fd95951..bfbed15ec 100644 --- a/app/templates/client/app/main/main.controller(js).js +++ b/app/templates/client/app/main/main.controller(js).js @@ -1,33 +1,34 @@ 'use strict'; + (function() { -function MainController($scope, $http<% if (filters.socketio) { %>, socket<% } %>) { - var self = this; - this.awesomeThings = []; - - $http.get('/api/things').then(function(response) { - self.awesomeThings = response.data;<% if (filters.socketio) { %> - socket.syncUpdates('thing', self.awesomeThings);<% } %> - }); -<% if (filters.models) { %> - this.addThing = function() { - if (self.newThing === '') { - return; - } - $http.post('/api/things', { name: self.newThing }); - self.newThing = ''; - }; - - this.deleteThing = function(thing) { - $http.delete('/api/things/' + thing._id); - };<% } %><% if (filters.socketio) { %> - - $scope.$on('$destroy', function() { - socket.unsyncUpdates('thing'); - });<% } %> -} - -angular.module('<%= scriptAppName %>') - .controller('MainController', MainController); + function MainController($scope, $http<% if (filters.socketio) { %>, socket<% } %>) { + var self = this; + this.awesomeThings = []; + + $http.get('/api/things').then(function(response) { + self.awesomeThings = response.data;<% if (filters.socketio) { %> + socket.syncUpdates('thing', self.awesomeThings);<% } %> + });<% if (filters.models) { %> + + this.addThing = function() { + if (self.newThing === '') { + return; + } + $http.post('/api/things', { name: self.newThing }); + self.newThing = ''; + }; + + this.deleteThing = function(thing) { + $http.delete('/api/things/' + thing._id); + };<% } if (filters.socketio) { %> + + $scope.$on('$destroy', function() { + socket.unsyncUpdates('thing'); + });<% } %> + } + + angular.module('<%= scriptAppName %>') + .controller('MainController', MainController); })(); diff --git a/app/templates/client/components/auth(auth)/auth.service(js).js b/app/templates/client/components/auth(auth)/auth.service(js).js index 2a1fcb480..eb687a564 100644 --- a/app/templates/client/components/auth(auth)/auth.service(js).js +++ b/app/templates/client/components/auth(auth)/auth.service(js).js @@ -1,7 +1,8 @@ 'use strict'; -angular.module('<%= scriptAppName %>') - .factory('Auth', function Auth($http, User, $cookies, $q) { +(function() { + + function AuthService($http, User, $cookies, $q) { /** * Return a callback or noop function * @@ -18,7 +19,7 @@ angular.module('<%= scriptAppName %>') currentUser = User.get(); } - return { + var Auth = { /** * Authenticate user and save token @@ -42,10 +43,10 @@ angular.module('<%= scriptAppName %>') return user; }) .catch(function(err) { - this.logout(); + Auth.logout(); safeCb(callback)(err.data); return $q.reject(err.data); - }.bind(this)); + }); }, /** @@ -71,9 +72,9 @@ angular.module('<%= scriptAppName %>') return safeCb(callback)(null, user); }, function(err) { - this.logout(); + Auth.logout(); return safeCb(callback)(err); - }.bind(this)).$promise; + }).$promise; }, /** @@ -130,7 +131,7 @@ angular.module('<%= scriptAppName %>') return currentUser.hasOwnProperty('role'); } - return this.getCurrentUser(null) + return Auth.getCurrentUser(null) .then(function(user) { var is = user.hasOwnProperty('role'); safeCb(callback)(is); @@ -150,7 +151,7 @@ angular.module('<%= scriptAppName %>') return currentUser.role === 'admin'; } - return this.getCurrentUser(null) + return Auth.getCurrentUser(null) .then(function(user) { var is = user.role === 'admin'; safeCb(callback)(is); @@ -167,4 +168,11 @@ angular.module('<%= scriptAppName %>') return $cookies.get('token'); } }; - }); + + return Auth; + } + + angular.module('<%= scriptAppName %>') + .factory('Auth', AuthService); + +})();