Meteor: how to call reRun helper function after collectionHandle.ready () true

This is a new version of my old question :

So, thanks to the help of Tom Coleman, I finally figured out how to check if the subscription () is ready or not.

My current code structure is as follows:

/client/app.js: eventsHandle = null; groupsHandle = null; // ... // First Deps.autorun(): // Does not depend on any Session var, should just run every time Deps.autorun(function() { eventsHandle = Meteor.subscribe("events", function() { console.log('Deps.autorun(): Events loaded'); }); }); // Second Deps.autorun(): // contains all subscriptions which are dependent on my Session var "ehash" Deps.autorun(function() { if(Session.get('ehash')) groupsHandle = Meteor.subscribe("groups", Session.get('ehash'), function() { console.log('Deps.autorun(): Groups loaded with ehash: ' + Session.get('ehash')); }); }); // ... 

Then I have specific .js and .html files for all the template material in a folder named:

 /client/views/ --> <page>.js: Template.x.dataLoaded = function() { if(Session.get('ehash')) if(eventsHandle && groupsHandle && eventsHandle.ready() && groupsHandle.ready()) { console.log('All data loaded!'); singleevent = Events.find({ehash: Session.get('ehash')}).fetch()[0]; return true; } } 

This dataLoaded wraps basically everything in the corresponding template and shows the contents when dataLoaded returns true, or shows a load scan.

The problem is that in many cases this does not work, because this data-loading code only runs once. Therefore, if two handles are NOT ready () at the time dataLoaded is started, the content will NEVER appear. In this case, I still see that everything console.log comes from the app.js file (material Deps.autorun ()), but the log "All data is loaded!" never a ride.

So my question is: how do I start repeating this code so that dataLoaded again so that the content ultimately displays?

Best wishes

+6
source share
4 answers

The problem can be solved simply by creating a dependency:

 var _dep = new Deps.Dependency(); Template.x.dataLoaded = function() { _dep.depend(); ... } function handler() { ... do.stuff(); _dep.changed(); } 

Now, every time you run the _dep.changed() method, the helper will be re-run. Plain!

+8
source

To call the ready() eventHandle , you need brackets, otherwise I think you are just checking to see if the ready method exists. A discussion of this is here .

This will probably leave you with a problem eventHandle if eventHandle set to different signature descriptors in different parts of your javascript. Try to structure your application files as described here .

0
source

Yes, you need to call this.ready() manually. See the example in the documentation .

0
source

It looks harder than necessary. Maybe something is missing something for me, but it looks like you're just trying to install a boot loader when loading data. I posted a simple, more readable way of showing "downloads" in Meteor .

0
source

Source: https://habr.com/ru/post/951424/


All Articles