2015-12-24 187 views
1

我有兩個電話,用於匹配和匹配。匹配獲取一個特定匹配的數據,並匹配整個批次,以及一些相對評級。我需要將這些相對評分傳遞給匹配的特定匹配。從不同的API端點來自兩個控制器的角度連接數據

function matchService($http, $q, API_URL) { 

function getMatchesForVacancy(vacancyId) { 
    var deferred = $q.defer(); 

    $http.get(API_URL + vacancyId + '/matches') //v2.0 lib 
    .then(function matchesArrived(matchData) { 
     deferred.resolve(matchData.data); 
    }, function matchesFailed(error) { 
     deferred.reject(error); 
    }); 
    return deferred.promise; 
} 

function getMatchForVacancy(vacancyId, accountId) { 
    var deferred = $q.defer(); 
    $http.get(API_URL + vacancyId + '/matches/' + accountId) 
    .then(function matchesArrived(matchData) { 
     //matchData.data.candidateRating = {overallScore: 0.65}; - ie. what I need to get 
     deferred.resolve(matchData.data); 
    }, function matchesFailed(error) { 
     deferred.reject(error); 
    }); 
    return deferred.promise; 
} 

匹配和匹配返回JSON數據

服務顯示在不同的看法,所以我有單獨的控制器文件。現在在單匹配控制器中,我試圖通過特定的評分,其中id是相同的。

所以我試圖做的,是在候選人控制器創建這樣一個功能:

function connectRating(vacancyId, match){ 
    var matches = matchService.getMatchesForVacancy(vacancyId); 
    for(var i = 0; i < matches.length; i++){ 

    if(matches[i].accountId === match.accountId){ 
     match.candidateRating.overralScore = matches[i].candidateRating.overralScore; 
     return match; 
    } 
    } 
} 

火柴的JSON是這樣的:

[ 
    { 
    "accountId": 0, 
    "candidateRating": { 
     "overallScore": 0 
    } 
    } 
    ] 

不知怎麼正確的數據是不通過,或任何事情。我一定錯過了一些東西,所以我會很感激任何幫助。

回答

2

你錯過了諾言的成功功能。這就是你對異步結果的保證。試試這個:

matchService.getMatchesForVacancy(vacancyId).then(function(d){ 
    // the return data is in "d" 
    // do whatever you want with it 
});