2014-07-03 104 views
0

所以这里是我需要做的。我在我的代码中使用了Angular Deferred Bootstrap库,因为我需要在引导并尝试加载内容之前从RESTful服务器接收一些基本数据。无论如何,一旦第一个电话解决,我必须拨打第二个电话。第二个调用是依赖于第一个响应中包含的某个URL的登录。

现在,我想尝试一次接收数据(我正在尝试使用.success()块)时进行登录调用,但是一旦第一次调用解析,程序就会在登录调用完成之前开始bootstrapping ;因为我没有在服务器上“登录”,所以事情破裂了。

window.deferredBootstrapper.bootstrap({ 
       element: window.document.body, 
       module: 'app', 
       resolve: { 
        STARTUP_CONFIG: ['$http', function($http) { 
         return $http.get(url1 + 'blah/blah/blah/' + layout); 
        }], 

        CONTEXT_CONFIG: ['$http', function($http) { 
         return $http.get(url1 + 'blah/blah/blah/blah'). 
         success(function(data) { 
          $http.post('https://' + data.url2 + '/10001/guestidentity?client_id=' + data.id). 
          success(function(result){ 
           token = result.token; 
          }); 
         }); 
        }], 
       } 
      }); 

任何人都知道我能做什么吗?因为这

+0

'但是一旦第一次调用解决,它会在登录调用完成之前返回已解决的承诺。呃....否...立即返回未解决的承诺,然后当它解决时,第二个启动。 –

+0

我的意思是,延迟引导程序库阻止我的应用程序自引导,直到承诺解决。 – Fowarek

+0

问题在于你要返回一个代表第一个$ http而不是第二个的承诺。您并不在乎第一次解决问题的时间,因为您希望在第二次解决问题后继续解决问题,而不是第一次解决问题。达扬的答案处理了这一点。 –

回答

1

是喜是一个承诺,你可以链的下一个调用的函数,然后像

$http(url). 
    then(function(response){ 
      //note that this shouldn't be the body of the response 
      // but the entire response stream so you need to check for the status code and the body and apply and possibly apply any transformation that is needed as it will probably be just text 
      // then we can start the other call 
      return $http(url); 
    }) 

这种方式,第二承诺将被处理到最终目的地。

+0

除了使用'response.data.token'而不是'response.token'之外,他不应该做任何转换。 –