【问题标题】:Meteor and AngularJS reactive subscriptionsMeteor 和 AngularJS 响应式订阅
【发布时间】:2015-01-19 12:02:50
【问题描述】:

我有一个依赖于客户参数的出版物。因此,在从客户端订阅时,我需要将此参数发送到服务器。 我正在使用 angular-meteor 包并找到 $subscribe 包装器。 用法如下:$subscribe.subscribe(name, publisherArguments) 我正在尝试将动态 $scope 值传递给订阅,但它似乎不起作用。例如,以下示例从不提醒“您订阅了!”

   $subscribe.subscribe('aPublication',$scope.parameter).then(function(){
         alert("You subscribed !");
  });

假设服务器端是这样的

  Meteor.publish("aPublication", function (parameter) {
    ACollection.find({'aProperty':'parameter'}) });

我应该怎么做才能使$scope.parameter 的工作方式与我使用Session.get('parameter') 的方式相同?

【问题讨论】:

    标签: angularjs meteor


    【解决方案1】:

    @Flavien Volken,这是一个非常好的解决方案,但在我们新的 0.6.0 版本中,您也可以使用更类似于 Meteor 方式的解决方案来实现它,使用 scope.getReactively - http://angularjs.meteor.com/api/getReactively

    所以在你的解决方案中:

    $meteorUtils.autorun($scope, function() {      
       $meteorSubscribe.subscribe('aPublication',
                                  $scope.getReactively('parameter'))
                                 .then(function(){
                                      alert("You subscribed !");
          });
    });
    

    【讨论】:

      【解决方案2】:

      这是我的工厂,它确实有这个形状,因为我将它绑定到一个

         angular.module('myApp.controllers').factory('items', function () {
      
              var listOfItems = [
                  {name: "one"},
                  {name: "two"},
                  {name: "three"}];
      
              var currentItem = listOfItems[1];
      
              return {
                  'list': listOfItems,
                  'current': currentItem
              };
          });
      

      这里是我的控制器,我主要观察 city.current 值的变化,然后触发 currentItemChanged 函数,该函数将使用正确的参数进行订阅。

      angular.module('myApp.controllers').controller("MyCtrl", ['$scope', 'items', '$subscribe',
          function ($scope, items, $subscribe) {
      
                  $scope.$watch(function () {
                      return cities.current;
                  }, function (currentItem)
                  {
                      currentItemChanged(currentItem.name);
                  }, true);
      
                  function currentCityChanged(itemName)
                  {
                      // resubscribe to the right set of
                      $subscribe.subscribe('anItemsSubscription', itemName).then(function ()
                      {
      
                      });
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-07-09
        • 2017-07-29
        • 2017-11-30
        • 2016-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多