2016-03-04 44 views
0

我有一個companies集合,其中country_id字段指的是_id中的countries集合。如何在Restularular中模擬AngularJS中的一對一關係

Companies如下:

{ 
    name: "acme", 
    address: "zossener straße 123", 
    postalCode: "10961", 
    city: "berlin", 
    country_id: "56d58d68ab68b5cf3f72788e" 
} 

Countries如下:

{ 
    _id: "56d58d68ab68b5cf3f72788e", 
    name: "Germany" 
} 

我想要做的,是取代的Companiescountry_idCountries.name。因此,對於每個公司我想要國家名稱,而不是國家/地區ID。我正在使用Restangular。我的控制器:

// creates a Restangular object of the 'companies' endpoint for later use 
var Companies = Restangular.all('companies'); 

// for later pushing the countries 
$scope.allCompanies = []; 

// queries companies collection 
Companies.getList().then(function(companies) { 
    $scope.companies = companies; 

    // iterates each company to get the country name 
    for (var i = 0; i < $scope.companies.length; i++) { 
    // prepares temp object to be pushed to $scope.allCompanies 
    var buffer = { 
     name: $scope.companies[i].name, 
     address: $scope.companies[i].address, 
     postalCode: $scope.companies[i].postalCode, 
     city: $scope.companies[i].city, 
     countryId: $scope.companies[i].country_id 
    }; 

    // queries countries collection passing country_id 
    var Country = Restangular.one('countries', $scope.companies[i].country_id); 

    Country.get().then(function(country) { 
     // sets country name for temp object based on country_id 
     buffer.country = country.name; 

     // pushes buffer to $scope.allCompanies 
     $scope.allCompanies.push(buffer); 
    }); 
    }; 

運行時,$scope.allCompanies顯示了只有一個國家,這是分配給buffer最後一個國家的所有公司。我認爲承諾有問題,但不確定。任何幫助?提前致謝。

回答

0

您可以將檢索國家的邏輯封裝在函數中。這樣,該功能可以保持buffer的正確參考值,而不會受到i變化的影響:

// creates a Restangular object of the 'companies' endpoint for later use 
var Companies = Restangular.all('companies'); 

// for later pushing the countries 
$scope.allCompanies = []; 

// queries companies collection 
Companies.getList().then(function(companies) { 
    $scope.companies = companies; 
    function setCountryName(buffer){ 

    // queries countries collection passing country_id 
    var Country = Restangular.one('countries', buffer.countryId); 

    Country.get().then(function(country) { 
     // sets country name for temp object based on country_id 
     buffer.country = country.name; 
    }); 
    return buffer; 
    } 

    // iterates each company to get the country name 
    for (var i = 0; i < $scope.companies.length; i++) { 
    // prepares temp object to be pushed to $scope.allCompanies 
    var buffer = { 
     name: $scope.companies[i].name, 
     address: $scope.companies[i].address, 
     postalCode: $scope.companies[i].postalCode, 
     city: $scope.companies[i].city, 
     countryId: $scope.companies[i].country_id 
    }; 

     // pushes buffer to $scope.allCompanies 
     $scope.allCompanies.push(setCountryName(buffer)); 
    }; 
+0

完美地工作!你能解釋一下爲什麼我的代碼不工作嗎?我對此並不十分清楚。 –

+1

您的循環在完成對「國家」的請求之前正在更改「i」的值(因爲它是異步的)。這使得在答案之前「緩衝區」的引用發生了變化,然後承諾會更新一個不同的「緩衝區」變量。這使得結果變得有趣。通過使代碼調用'國家'保持參考,你可以避免這一點。 –

相關問題