kroe
kroe

Reputation: 1126

How to get all subscription data before rendering and then just update the dom when new data is received?

I currently havea widget which grabs hundreds of documents from the DB via subscription and then keep listening for new documents, so it can update a stock chart.

There is a problem tough, which is every time the data is updated the chart is updated, which causes a redraw.

This is a problem cause it's calling redraw hundreds of time at the beginning even tough it just need to "fetch all data then draw and wait for updates", the updates will then happen not very often, so then it would be ok to redraw.

my current code:

Template.nwidget.onRendered(function() {
  return this.autorun(function() {
    var data;
    data = {};
    data = Data.find({
      type: 'my_type'
    });
    data = data.fetch();
    return update(data);
  });
});

Upvotes: 0

Views: 411

Answers (1)

Pankaj Jatav
Pankaj Jatav

Reputation: 2184

For doing some after data subscription you can do like this:

Meteor.subscribe( 'collection', {
    onStop:  function( error /* optional */ ) {
        // when the sub terminates for any reason,
        // with an error argument if an error triggered the stop
    },
    onReady: function() {
        // when ready
    }
});

If you want to render page after the data subcribe then you can add waitOn in your router.

There is one more way to check where subscription is ready or not. If subscription is not ready you can show something else like a loading screen.

var handle = Meteor.subscribe( 'collection');

Tracker.autorun(function() {
  if (handle.ready())
      //write whatever you want to do here.
});

For the auto update in your view you can store the date in a reactive thing its may reactive var, Session or collection. Then you can return there values from helper to view. And that will auto update your view.

Upvotes: 3

Related Questions