0
我CoffeeScript的代码看起来像这样
.controller('SignInController',
($scope, CONFIG, restAuth, cookieAuth) ->
$scope.signInData = {}
$scope.res = {}
# TODO: Refactor this to service
$scope.processRegistration = ->
console.log($scope.signInData)
restAuth.post('signin', $scope.signInData)
.then(((data) ->
if data.res >= 0
$scope.res.signInSuccess = true
$scope.res.msg = 'You finished login successfully.'
cookieAuth.setCookie(data)
else
$scope.res.signInSuccess = false
$scope.res.msg = 'Your login failed. (#{ data.description })'
),
(->
$scope.res.signInSuccess = false
$scope.res.msg = 'Sorry, it seems that the server is not responding. Please try again later!')
)
return
)
.controller('SignUpController',
($scope, $http, CONFIG) ->
$scope.signUpData = {}
$scope.res = {}
# TODO: Refactor this to service
$scope.processRegistration = ->
$http
.post('#{ CONFIG.ROOT }/auth/signup', $scope.signUpData)
.success (data) ->
if data.res >= 0
$scope.res.signUpSuccess = true
$scope.res.msg = 'You finished registration successfully.'
else
$scope.res.signUpSuccess = false
$scope.res.msg = 'Your registration failed. (#{ data.description })'
.error(->
$scope.res.signUpSuccess = false
$scope.res.msg = 'Sorry, it seems that the server is not responding. Please try again later!'
)
return
)
可以看出,上有$scope
许多操作,它看起来如何重构这个有一点笨拙..有没有人有什么建议?
把它们的服务。如果您在html中使用该函数,则所有范围函数都可以调用服务函数。 – Gustav 2015-02-12 07:01:49