6

This a new version of my old question:

So thanks to Tom Coleman's help I finally figured out on how to properly check if a subscription is ready() or not.

My current code structure looks like this:

/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 view specific .js and .html files for all the template stuff in a folder called:

/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 helper dataLoaded wraps basically everything in the corresponding template and shows the content when dataLoaded returns true or else shows a loading spinner.

Problem is that in many cases this does not work because this dataLoaded code is only run once. So if the two handles are NOT ready() at the time dataLoaded is run, content will NEVER show up. In this case I still see all the console.log's coming from the app.js file (the Deps.autorun() stuff) but the log "All data loaded!" is never echod.

So my question is: How do I trigger a rerun of this code so dataLoaded is run again so content will eventually show up?

best regards

Patrick DaVader
  • 2,133
  • 4
  • 24
  • 35

4 Answers4

9

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 _dep.changed() method, the helper will rerun. Simple!

Hubert OG
  • 19,314
  • 7
  • 45
  • 73
  • Thanks for your answer! For some reason this helps whe I am doing a reload on a page via Cmd+L (jump into url bar) + Enter. (so like typing in the same URl again and load). But IF I am doing a reload via Cmd+R, I end up with errors like this one: http://cl.ly/image/2C2p0Y1F030Y – Patrick DaVader Aug 13 '13 at 20:13
  • You had it better before: `if(eventsHandle && groupsHandle && eventsHandle.ready() && groupsHandle.ready())`. – Hubert OG Aug 13 '13 at 20:16
  • Why can't the standard handlers take care of this? Isn't that why we have them? – Jonatan Littke Aug 27 '13 at 14:03
0

To call the ready() method of eventHandle you need the parentheses otherwise I think you are just checking that the ready method exists. Discussion of this is here.

This will probably still leave you with a problem if eventHandle is being set to various subscription handles in different parts of your javascript. Try structuring the files of your app as described here.

Community
  • 1
  • 1
user728291
  • 4,138
  • 1
  • 23
  • 26
  • Thanks for making the parentheses thingy clear! About the structure: I tried putting all my subscriptions into ONE Deps.autorun() for the whole meteor project (that is what you mean, right?!). Problem is that I can't access the handles then in my view specific .js files (--> `client/views/ – Patrick DaVader Aug 10 '13 at 20:50
0

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

Mitar
  • 6,756
  • 5
  • 54
  • 86
0

This looks more complicated than it needs to be. I'm quite possibly missing something, but it seems to me you're just trying to set a loading spinner when data is loaded. I've posted a simple, more readable way of showing "loading" in Meteor.

BenjaminRH
  • 11,974
  • 7
  • 49
  • 76
  • Well I tried this approach already but for some reason it wouldn't work properly. Maybe I did something wrong, gonna try it again tomorrow! Gonna keep you posted! :) – Patrick DaVader Aug 14 '13 at 21:30