【发布时间】:2015-02-12 16:00:46
【问题描述】:
我的咖啡脚本代码如下所示
.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 中使用该函数,可能所有范围函数都会调用服务函数。
标签: javascript angularjs coffeescript