0

Im using the FlowRouter in my app and new to Meteor. I have subscribed to my collection like this :

Template.PanelEditAbout.onCreated(function() {
    var self = this;
    self.autorun(function() {
        self.subscribe('pages', 'about');
    });
});

Im trying to use the subscription in the rendered function but it's not working :

Template.PanelEditAbout.rendered = function() {
    page = Pages.findOne({
        slug: 'about'
    });
}

If im correct, I have to wait for the subscription to be available. How can I do this? I also need to add a loading msg (or a spinner) while it's getting ready. I know how to do this in IronRouter but not with the FlowRouter.

THpubs
  • 7,804
  • 16
  • 68
  • 143

2 Answers2

3

Don't ever subscribe in onRendered, first off. Try the below:

Template.PanelEditAbout.onCreated(function() {
  this.subscribe('pages', 'about');
});

Template.PanelEditAbout.onRendered(function() {
  let page = {};

  this.autorun(() => {
    if (this.subscriptionsReady()) {
      console.log('subs ready');
      page = Pages.findOne({
        slug: 'about'
      });
    }

    console.log(page);
  });
});
ffxsam
  • 26,428
  • 32
  • 94
  • 144
1

You can use the onReady during your subscribtion to page if it is the name of your publication.

Template.PanelEditAbout.rendered = function() {
  Meteor.subscribe("page", Yourslug,{
    onReady: function () { console.log("onReady And the Itemns actually Arrive",     arguments); },
    onError: function () { console.log("onError", arguments); }
  });
};

the console log is just an exemple.

Z. Clément
  • 474
  • 4
  • 15
  • If you speak about loading message like a real time tchat I think you have to use reactivars and helpers. – Z. Clément Nov 17 '15 at 07:00
  • Everything is working but when I add a loading msg, the CKEDITOR function can't find the text area – THpubs Nov 17 '15 at 07:21
  • Ok so, in the other answer he is right do the suscription and wait in the onCreated, and use an helper to format your data and next you put in your html page . I m not totally sure, but try it. – Z. Clément Nov 17 '15 at 07:49
  • Amazing! This was exactly what I needed to do a recursive subscription model. I had to ensure that data A was available, before loading data B and C, and finally D which relied on C. – Andy Jun 02 '17 at 01:45