Meteor 1.0.4 Template.subscribe and DDP.connect


#1

I am welcoming the new change in 1.0.4 that brings a subscribe method to Templates which works exactly as Meteor.subscribe and automatically stops the subscription when the template is destroyed. This should make for much cleaner code.

However, how am I supposed to use this new feature when I am connecting to a remote server via DDP.connect?


#2

Good point! That is an oversight in the current API - we should add an option to use a different connection.

For now, as a workaround, you can use regular Meteor.subscribe inside this.autorun, which has a very similar effect. You won’t get the subscriptionsReady method, but it’s a start.

Could you file a feature request on GitHub for this?


#3

I just went through the source and saw that in blaze/template.js it defines subscribe as this.view.subscribe, and in blaze/view.js Blaze.View.prototype.subscribe it actually checks in options for connection

Blaze.View.prototype.subscribe = function (args, options) {
  var self = this;
  options = {} || options;

  self._errorIfShouldntCallSubscribe();

  var subHandle;
  if (options.connection) {
    subHandle = options.connection.subscribe.apply(options.connection, args);
  } else {
    subHandle = Meteor.subscribe.apply(Meteor, args);
  }

  self.onViewDestroyed(function () {
    subHandle.stop();
  });

  return subHandle;
};

so this tells me that I should be able to do the following in Template.created

Template.hello.created = function() [
 this.subscribe('subscription', arg1, arg2, {connection: DDP.connect(url)}, function(){
  console.log('successfully subscribed to subscription on '+url);
 });
}

right?


#4

And it does not work. Apparently creates a recursion of some sort and browser freezes

Exception from Tracker afterFlush function: Maximum call stack size exceeded

#5
Meteor.remoteConnection = DDP.connect(url);

Template.hello.created = function() [
 this.subscribe('subscription', arg1, arg2, {
  connection: Meteor.remoteConnection,
  onReady: function(){
   console.log('successfully subscribed to subscription on '+url);
  },
  onError: function(err) {
   console.log(err);
  }
 });
}

shows the error

Meteor.makeErrorType.errorClass {error: 404, reason: "Subscription not found", details: undefined, message: "Subscription not found [404]", errorType: "Meteor.Error"…}

but when I use Meteor.remoteConnection.subscribe('subscription') all works as expected


#6

Try this:

this.view.subscribe(['subscription', arg1, arg2],
  { connection: Meteor.remoteConnection });

Basically we added it as an internal API to views, but made the API more extensible by taking the arguments as an array. I actually forgot we did this, but it was @glasser’s idea to address exactly this case.


#7

Still no go. Doing it that way does not create an Error, but also doesn’t subscribe.


#8

I filed an issue on github, Maybe it is a defect, and best case scenario a matter of documentation


#9

Quick update on this issue. It is actually a bug and I have submitted a pull request with a fix.


#10

Does anyone know what’s the status of this feature? I’ve just run into exactly the same problem.


#11

Pull request successfully merged and closed


#12

Yeah, but for some reason it’s not yet working in 1.1.0.3. I looked into the source code and the

options = {} || options;

snippet is still there :wink:


#13

Too bad. If I remember correctly, that was the only place I found that in the code and pointed out that it doesn’t work and changed it in my pull request …


#14

Yeah, 1.2 has been taking a while, but we are doing our best. 1.1.0.3, as the small version change suggests, was only a fix for facebook login after they changed the API.


#15

@sashko BTW, is there an official release date for 1.2?