2014-12-02 64 views
0

我在Ionic移動項目中使用角度時遇到了問題,我的這段代碼在我的桌面環境中工作得很好。我以500ms的間隔對另一個Web服務器進行三次API調用。但在使用較低連接的電話中,有時會有一個呼叫失敗,因此整個流程都會失敗。 在上一次完成的時刻,是否有某種方法可以進行API調用?我的意思是,沒有使用固定的時間。angularJS中的API調用彼此正確

//get member information 
$timeout(function() { 
    membersService.getMember(community.url, community.user.api_key, community.user.auth_token, $stateParams.userId). 
    success(function(data) { 
    $scope.member = data.result; 
    }); 
}, 500); 

$timeout(function() { 
    messagesService.getConversation(community.url, community.user.api_key, community.user.auth_token, community.user.user_info.guid, $stateParams.userId, "from"). 
    success(function(data) { 
    if(data.result["entities"].length > 0) { 
     messages = messages.concat(data.result["entities"]); 
     subject = "Re: "+data.result["entities"][data.result["entities"].length-1].title; 
    } 
    $scope.messageData.subject = subject; 
    }); 
}, 1000); 

$timeout(function() { 
    messagesService.getConversation(community.url, community.user.api_key, community.user.auth_token, community.user.user_info.guid, $stateParams.userId, "to"). 
    success(function(data) { 
    log = []; 
    if(data.result["entities"].length > 0) { 
     angular.forEach(data.result["entities"], function(v, k) { 
     v.owner_guid = $stateParams.userId; 
     log.push(v); 
     }, log); 
     messages = messages.concat(log); 
    } 
    var log = []; 
    var count = 0; 
    angular.forEach(messages, function(v, k) { 
     messages_reorder.push(v); 
    }, log); 
    }); 
}, 1500); 

回答

1

這是實現嵌套承諾鏈的結果:

var loadMemberInfo = function(userId) 
{ 
    return membersService 
    .getMember(community.url, community.user.api_key, community.user.auth_token, $stateParams.userId) 
    .then(function(data) 
    { 
     $scope.member = data.result; 
    }); 
}, 
getConversationFrom = function() { 
    return messagesService 
    .getConversation(community.url, community.user.api_key, community.user.auth_token, community.user.user_info.guid, $stateParams.userId, "from") 
    .then(function(cf) { 
    if(cf.data.result["entities"].length > 0) { 
     messages = messages.concat(cf.data.result["entities"]); 
     subject = "Re: "+cf.data.result["entities"][cf.data.result["entities"].length-1].title; 
    } 
    $scope.messageData.subject = subject; 
    }); 
}, 
getConversationTo = function() { 
    messagesService 
    .getConversation(community.url, community.user.api_key, community.user.auth_token, community.user.user_info.guid, $stateParams.userId, "to") 
    .then(function(ct) { 
    log = []; 
    if(ct.data.result["entities"].length > 0) { 
     angular.forEach(ct.data.result["entities"], function(v, k) { 
     v.owner_guid = $stateParams.userId; 
     log.push(v); 
     }, log); 
     messages = messages.concat(log); 
    } 
    //order array 
    messages = messages.sort(function(a,b) { return a.time_created - b.time_created }); 

    var log = []; 
    var count = 0; 

    angular.forEach(messages, function(v, k) { 
     messages_reorder.push(v); 
    }, log); 
    }); 
}, 
orderFullConversation = function() { 
    $ionicLoading.hide(); 
    console.log(messages); 
    if(messages_reorder.length > 5) { 
    var messages_partial = messages_reorder.slice(messages_reorder.length-5,messages_reorder.length); 
    } 
    else { 
    var messages_partial = messages_reorder; 
    } 
    $scope.messages = messages_partial; 
    $scope.community = community; 
}; 


loadMemberInfo($stateParams.userId) 
    .then(getConversationFrom) 
    .then(getConversationTo) 
    .then(orderFullConversation); 

更多細節here

0

你應該看看Angular Promise API。它是爲了相同的目的。它可以讓你鏈AJAX的呼聲此起彼伏..

// this 
    $http.get('/api/v1/movies/avengers') 
     .success(function(data, status, headers, config) { 
     $scope.movieContent = data; 
     }); 

    // is the same as 
    var promise = $http.get('/api/v1/movies/avengers'); 

    promise.then(
     function(payload) { 
     $scope.movieContent = payload.data; 
     }); 

更多詳細信息here