2015-09-02 170 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>