numtel / meteor-mysql

Reactive MySQL for Meteor
MIT License
343 stars 41 forks source link

Template-Level Subscriptions #38

Open rosspeoples opened 9 years ago

rosspeoples commented 9 years ago

From what I've read, the new recommendation is to create subscriptions in the Template.onCreated() callbacks using the "this.subscribe()" function. Right now it would appear that using "new MysqlSubscription()" is the only way to subscribe to MySQL publications. Is there any way to get this to work using the standard "this.subscribe()" function inside Template.onCreated()?

Relevant Meteor docs here: http://docs.meteor.com/#/full/Blaze-TemplateInstance-subscribe

Thanks

numtel commented 9 years ago

That is correct, you must subscribe using the constructor new MysqlSubscription. Since this is not using this.subscribe(), you must stop the subscription manually in the Template.onDestroyed().

blitzlightnin commented 9 years ago

what if the template is not destroyed? instead, the arguments it takes is changed by some reactive sources like sessions. I'm facing with a "tracker exception error subscrption failed" error when im trying to make MysqlSubscription in Template.onCreated autorun passing a param from FlowRouter.getQueryParam(which is reactive).

danielgrabowski commented 8 years ago

I have the same problem using change() function. Sobetimes it work's and sobetimes I get the "Subscription failed" error.

numtel commented 8 years ago

@danielgrabowski Can you post a link to a repo containing the minimum code necessary to reproduce the issue?

Aur0r commented 8 years ago

As a response to @blitzlightnin - This is what I did for Template-Level Subscriptions:

Template.logEntries.created = function() {
  this.computation = Tracker.autorun(() => {
    if (this.logEntries) {
      this.logEntries.stop();
      this.logEntries = null;
    }
    var from = Session.get("logentries.from");
    var till = Session.get("logentries.till");
    this.logEntries = new MysqlSubscription('logEntries', {from: from, till: till});
  });
};

Template.logEntries.destroyed = function() {
  this.computation.stop();
  this.logEntries.stop();
};

Template.logEntries.helpers({
  logEntries: function () {
    Session.get("logentries.from");
    Session.get("logentries.till");
    return Template.instance().logEntries.reactive();
  }
});