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