iron-router wait on Collection.findOne() as data object before render
Asked Answered
D

1

6

I am looking for a solution that iron-router is waiting for a successfully find method on my collection before rendering.

My route looks like this:

this.route('business', {
        path : '/business/:type/:_id',
        waitOn : function () {
            return Meteor.subscribe('business', this.params._id);
        },
        data : function () {
            return Business.findOne({_id: this.params._id});
        }
    });

This works fine. It seems that iron-router is waiting for the subscribe of the Collection to get the right Document back for the client. But the data which i need in my template has a delay for the findOne function.

Template.businessItemItem.rendered = function () {
    console.log(Router.current().data()); // undefined
    window.setTimeout(function(){
      console.log(Router.current().data()); // [Object]
    }, 1000);
}

Solution For everyone with the same problem. Just add the "action" method for your route like this:

action : function () {
   if (this.ready()) this.render();
}

With this method everything works fine for me.

Dazzle answered 13/4, 2014 at 2:26 Comment(3)
You are my hero! This was driving me crazy. Your solution is perfect, but I'll investigate more: if it's a bug in iron-router we should open an issue on github. Thanks!Arabian
Yes i think it does not work the way it should. reference: github.com/EventedMind/iron-router/blob/dev/… It says that the waitOn method is waiting for the data before rendering and returns it - but this is not what i currently do ...Dazzle
A quick update to add that I had a case when data() function was called before (and then after) action() method (if I refresh from the browser causing the route to reload). In this case even this solution isn't working :-( so I switched to if (!this.ready()) return; put as the first line of data() method, as suggested in this post: groups.google.com/forum/#!topic/meteor-talk/lK3v9ZxIbcoArabian
U
2

I'm not sure if I get your problem, but if I do, you should read about Controlling subscriptions, and especially Router.onBeforeAction('loading') . Now, you're reinventing the wheel.

Unleash answered 13/4, 2014 at 7:55 Comment(6)
Thanks for your comment but please see my solution i already edited to my question. The action method with this.ready in use works fine :)Dazzle
Sure it works, because you are basically recoding the default "loading" hook, which is better because it applies to every route, with your solution, one would have to copy/paste this "action" code on every route.Commit
I have the same issue. I tried using the waitOn() hook and it seemed to do nothing. I was returning an array of a couple of subscribe() calls, which is supposed to make the route wait, but it did not. Timo's solution worked for me though. Maybe there's a bug the the iron-router.Misinform
@Jachin, You're sure you have the line Router.onBeforeAction('loading') in your code?Unleash
Same issue here. @PeppeL-G I have Router.onBeforeAction('loading') in code but it's a different problem. Sure @Timo solution is recoding something that "should" already be there, but in some circumstances it seems not. I'll try to isolate a case and put a repo on github.Arabian
@Misinform - I had the same problem - i fixed it with adding Router.configure { loadingTemplate: "loading" }Oubliette

© 2022 - 2024 — McMap. All rights reserved.