Flavien Volken
Flavien Volken

Reputation: 21359

Meteor and AngularJS reactive subscriptions

I've a publication which relies on a client's parameter. Therefore, while subscribing from the client I need to send this parameter to the server. I'm using the angular-meteor package and found the $subscribe wrapper. The usage is as follow: $subscribe.subscribe(name, publisherArguments) I am trying to pass dynamic $scope values to the subscription but it does not seems to work. For example the following example never alerts "You subscribed !"

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

assuming the server side looks like this

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

What should I do to make the $scope.parameter works the same way as if I was using Session.get('parameter') ?

Upvotes: 1

Views: 688

Answers (2)

Urigo
Urigo

Reputation: 3185

@Flavien Volken, that's a really nice solution but in our new 0.6.0 version you can also do it with a solution a bit more similar to the Meteor way using scope.getReactively - http://angularjs.meteor.com/api/getReactively

So in your solution:

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

Upvotes: 1

Flavien Volken
Flavien Volken

Reputation: 21359

Here is my factory, it do have this shape as I'm binding it to a

   angular.module('myApp.controllers').factory('items', function () {

        var listOfItems = [
            {name: "one"},
            {name: "two"},
            {name: "three"}];

        var currentItem = listOfItems[1];

        return {
            'list': listOfItems,
            'current': currentItem
        };
    });

Here is then my controller, I basically watch for changed for the cities.current value to then fire the currentItemChanged function which will subscribe with the right parameters.

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 ()
                {

                });
            }

Upvotes: 1

Related Questions