Skip to content

Instantly share code, notes, and snippets.

@kubakrzempek
Created July 3, 2015 10:25
Show Gist options
  • Save kubakrzempek/f1097643ac723604d42c to your computer and use it in GitHub Desktop.
Save kubakrzempek/f1097643ac723604d42c to your computer and use it in GitHub Desktop.
lazy login
# Storage service
angular.module('app').service 'Storage', ($rootScope) ->
$rootScope.storage = undefined
this.keep = (ctrl, func, params...) ->
$rootScope.storage = {
ctrl: ctrl,
func: func,
params: params
}
this.flush = ->
if $rootScope.storage?
$rootScope.$broadcast 'flushingStorage', $rootScope.storage
$rootScope.storage = undefined
this
# Question controller
angular.module('app').controller 'questionCtrl', ($rootScope, $scope, $state, Answers, Comments, Storage) ->
$scope.saveAnswer = (id, type, content) ->
if content?
Answers.new(id, type, content)
.success ->
$scope.content = ''
.error (data, status) ->
if status == 401
Storage.keep('questionCtrl', 'saveAnswer', id, type, content)
$state.go('.loginRegister')
$scope.saveComment = (id, type, content) ->
if content?
Comments.new(id, type, content)
.success ->
Questions.single($scope.id).success (data) ->
$scope.question = data
$scope.content = ''
.error (data, status) ->
if status == 401
Storage.keep('questionCtrl', 'saveComment', id, type, content)
$state.go('.loginRegister')
$scope.$on 'flushingStorage', (event, storage) ->
if storage.ctrl == 'questionCtrl'
params = storage.params
switch storage.func
when 'saveAnswer'
$scope.saveAnswer(params[0], params[1], params[2])
when 'saveComment'
$scope.saveComment(params[0], params[1], params[2])
when 'saveStoryForLater'
$scope.saveStoryForLater(params[0])
# Login Controller
angular.module('app').controller 'loginCtrl', ($scope, Sessions, Storage) ->
$scope.login = ->
$scope.logging = true
Sessions.signin($scope.user)
.success (user) ->
Sessions.setCurrentUser(user)
$scope.$state.go('^')
$scope.logging = $scope.loggingError = false
Storage.flush()
.error (data) ->
$scope.logging = false
$scope.loggingError = true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment