0

我已经链接了一些我想按特定顺序执行的方法。第二种方法deviseLogin()返回一个 URL,应该在下一个方法中传递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
4

2 回答 2

0

您正在立即调用函数,而不是将它们作为回调传递。
defer.promise部分似乎也没有必要。

代码应如下所示:

$scope.signIn = (data) ->
    getUserToken(data.email, data.password)
    .then(deviseLogin)
    .then(redirect)
于 2014-08-01T07:38:03.600 回答
0

我从来没有最后一个加入。通过移动到 success() on.then( redirect(url) )解决了这个问题。redirect(url)deviseLogin()

感谢所有的建议和帮助!:)

于 2014-08-04T10:33:05.200 回答