Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 447
TJR Avatar asked Apr 13 '14 02:04

TJR


1 Answers

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.

like image 157
Peppe L-G Avatar answered Dec 06 '22 11:12

Peppe L-G