2
votes

I need to return my subscribtion in the waitOn parameter of iron-router after a async function is ready.

It should be work like this:

this.route('myRoute', {
    template : 'myTemplate',
    path : '/foo/:param',
    waitOn : function () {
        MyAsyncFunction(function(this.params.param){
            var result = 'whatever';
            return Meteor.subscribe('MyCollection', result);    
        });
    },
    data : function () {
        return MyCollection.find().fetch()
    }
});

What is the best way to solve this with iron-router ? I am looking for a recommend solution.

1

1 Answers

0
votes

You can run MyAsyncFunction from the onRun hook and use it to set a reactive variable that will trigger the subscription to update.

this.route('myRoute', {
    template : 'myTemplate',
    path : '/foo/:param',
    onRun: function () {
        MyAsyncFunction(function(this.params.param){
            Session.set('result', 'whatever');    
        });
    }, 
    waitOn : function () {
        return Meteor.subscribe('MyCollection', Session.get('result'));
    },
    data : function () {
        return MyCollection.find().fetch()
    }
});

The onRun hook will rerun when the param changes.