2016-03-04 42 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”的值(因为它是异步的)。这使得在答案之前“缓冲区”的引用发生了变化,然后承诺会更新一个不同的“缓冲区”变量。这使得结果变得有趣。通过使代码调用'国家'保持参考,你可以避免这一点。 –

相关问题