Good day.

I have a bunch of controllers. Every of them have common behavior: check 
user service (*sUser*) if logged in, and if not change location to */login*.

Currently it works like this (code duplication in every controller)

*ctrls.controller 'cProjectsList', ['$scope', '$http', '$location', 
'sLoadingIndicator', 'sUser', ($scope, $http, $location, sLoadingIndicator, 
sUser)->*

*  if not sUser.isLoggedIn()*
*    return $location.path('login')*

*  $http.get('getProjectsList')*
*  .success((response)->*
*    sLoadingIndicator.loaderOff()*
*  )*
*]*

It works, although i want to factor out this logic and somehow reuse in 
everywhere:

*if not sUser.isLoggedIn()*
*    return $location.path('login')*

But i cannot, because when user not logged in i need imitatively return 
from function to prevent any further execution. And i can do that only by 
issuing return statement from controller itself.

If i instead for example will intercept $routeChangeStart like that (the 
same, is with services):

*$rootScope.$on '$routeChangeStart', (event, next)->*
*    url = next.templateUrl*
*    if url != 'loginForm' and !sUser.isLoggedIn()*
*      $location.path('login')*

This successfully change route for not logged in user. But it wont stop 
logic in controller continue executing.

So in my example i have this piece of code in controller executed:

*$http.get('getProjectsList')*
*  .success((response)->*
*    sLoadingIndicator.loaderOff()*
*  )*


 Have i duplicate common logic everywhere, or are there any other variants?

Thank you in advance.









-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to