Kurounin / Pagination

This package allows you to paginate the subscriptions over meteor's collections. It can be used in a Blaze template or in ReactJS.
MIT License
56 stars 22 forks source link

Question: How to avoid a double call to autorun? #80

Closed carlosalvidrez closed 6 years ago

carlosalvidrez commented 6 years ago

I am placing a "this.pagination.ready()" within my template autorun, and I notice that it causes the autorun to execute twice. Is there a way to handle this so the autorun only gets called once? I realize I may be missing something : )

PS. Thank you for your patience and super helpful answers.

Kurounin commented 6 years ago

Do you have any other reactive variables that you use in the autorun? Autorun will be executed anytime one of the reactive variables change their value. If you're only using this.pagination.ready() it is probably called once when it is false and another time when it is true (you could ignore the first false by using a template variable (this.isInit = true/false))

carlosalvidrez commented 6 years ago

Thanks. Like this?

if( ! this.pagination.ready()) { console.log( "passage.created.js - autorun - this.pagination.ready() NOT:" , this.pagination.ready() ); this.isInit = false; }

Kurounin commented 6 years ago

Yes, you can write something like this to avoid the first autorun call:

Template.MyList.onCreated(function() {
  this.pagination = new Meteor.Pagination(Collection);
  const pagination = this.pagination;
  let isInit = true;

  this.autorun(function () {
    if (isInit) {
      isInit = false;
    } else {
     // execute code related to this.pagination.ready()
    }
  });
});
carlosalvidrez commented 6 years ago

Got it, thanks! Sorry, I thought this.isInit was invoking some existing meteor property : )