2015-09-02 267 views
1

來自沉重MySQLPHP背景我不能真正理解這一點。處理流星角

我有一個branches與ManytoOne關係Restaurants(所以有很多分支機構連接到同一家餐廳)的集合。

這樣的關係由字段restaurantId定義,其存儲對象的id。我branches收集如下顯示:

{ 
    "_id" : "uH4KbNYxw8cnCEqvk", 
    "address" : "1 High Street", 
    "loc" : { 
     "coordinates" : [ 
      0.0, 
      0.0 
     ], 
     "type" : "Point" 
    }, 
    "restaurantId" : "dvxZ2NiA3gbevffsk" 
} 

,當然還有restaurants現藏

{ 
    "_id" : "dvxZ2NiA3gbevffsk", 
    "name" : "Restaurant name" 
} 

,我使用$near查詢由接近下令所有的樹枝,我無法找到一個乾淨的在分支上循環時獲取父級餐廳名稱的方法。

這裏是我做過什麼 - 我的意思是,它的工作原理,因爲它是,但我認爲性能方面它有很多有待改進

// controller 
angular.module("MyApp").controller("SearchRestaurantsCtrl", 
    function($scope, $stateParams, $meteor){ 

     $meteor.subscribe('branches'); 
     $meteor.subscribe('restaurants'); 

     $scope.branches = $meteor.collection(function() { 
      return Branches.find({ 
       loc: { 
        $near: { 
         $geometry: { 
          type: "Point", 
          coordinates: [ $stateParams.lng, $stateParams.lat] 
         }, 
         $maxDistance: 300000 
        } 
       } 
      }); 
     }); 

     $scope.getRestaurant = function(restaurantId) { 
      return Restaurants.findOne({ 
       _id: restaurantId 
      }); 
     }; 
    } 
); 

// view 
<div ng-repeat="branch in branches"> 
    <h3>{{getRestaurant(branch.restaurantId).name}}</h3> 
    <address>{{branch.address}}</address> 
</div> 

回答

1

我結束了使用經過收集和濾波都使用角:

控制器

angular.module("sushisushi24").controller("SearchRestaurantsCtrl", 
    function($scope, $stateParams, $meteor){ 

     $scope.branches = $meteor.collection(Branches).subscribe('branchesAndRestaurants'); 
     $scope.restaurants = $meteor.collection(Restaurants); 
    } 
); 

流星發佈

Meteor.publish('branchesAndRestaurants', function(opts) { 

    branches = Branches.find(); 
    restaurantIds = branches.map(function(branch) { return branch.restaurantId }); 

    return [ 
     branches, 
     Restaurants.find({_id: {$in: restaurantIds}}) 
    ]; 
}); 

查看

<div ng-repeat="branch in branches"> 
    <div ng-repeat="restaurant in restaurants | filter: {_id:branch.restaurantId}"> 
     <h3>{{restaurant.name}}</h3> 
    </div> 
    <address>{{branch.address}}</address> 
</div>