我已经链接了一些我想按特定顺序执行的方法。
第二种方法deviseLogin()
返回应在下一个方法redirect(url)
第一种和第二种方法运行,但我从来没有听到过去redirect(url)
的任何内容。你能看出我做错了吗?
getUserToken = (username, password) ->
console.log "I'm first"
$http(
method: "POST"
url: "/oauth/token"
params: params
).success((data, status, headers, config) ->
$scope.$storage.token = data.access_token
defer = $q.defer()
$scope.signIn = (data) ->
defer.promise
.then( getUserToken($scope.email, $scope.password) )
.then( deviseLogin() )
.then( redirect(url) )
redirect = (url) ->
console.log "I'm last"
console.log "will redirect to #{url}"
$window.location = data.url
deviseLogin = () ->
console.log "I'm second"
$scope.authErrors = []
authData = { user: { email: $scope.email, password: $scope.password }}
$http(
# Perform a regular Devise login
method: "POST"
url: "/users/sign_in"
data: authData
)
.success (data) ->
# I can see the log lines below and data.url is what I expect it to be
console.log "logged in devise"
console.log "data.url: #{data.url}"
data.url
答案 0 :(得分:0)
您正在调用这些函数,而不是将它们作为回调传递
defer.promise
部分似乎也没必要。
代码应如下所示:
$scope.signIn = (data) ->
getUserToken(data.email, data.password)
.then(deviseLogin)
.then(redirect)
答案 1 :(得分:0)
我从来没有得到最后一个.then( redirect(url) )
。通过将redirect(url)
移到deviseLogin()
上的成功()内来解决问题。
感谢您的所有建议和帮助! :)