2

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.

TJR
  • 6,307
  • 10
  • 38
  • 64

1 Answers1

0

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.

Neil
  • 2,137
  • 16
  • 24
  • " runs just once when the route is first loaded. NOTE that this doesn't run again if your page reloads via hot-code-reload, so make sure any variables you set will persist over HCR (for example Session variables)." This seems not to be teh right way isnt it ? It do not has to be loaded once, it has to be loaded everythime I will reload the route or use another param but ALSO when using the same param. – TJR Aug 04 '14 at 07:58
  • 1
    @TimoRütten just updated to the latest Iron-Router which deprecates load in favour of onRun. It will run on a param change or page refresh but not on HCR. – Neil Aug 04 '14 at 16:05
  • Thanks for this information. I will try this tomorrow and will give a response. – TJR Aug 04 '14 at 17:58
  • Hm. It seems to work like you described but the problem is - and i really dont know why - this callback will be called 2 times and not one time per load or param-change. – TJR Aug 05 '14 at 19:04
  • Hey, i got the error "Route dispatch never rendered. Did you forget to call this.next() in an onBeforeAction?" but i didn´t defined an onBeforeAction function in this neither any route. Can you help?@Neil – Sven Delueg Apr 28 '15 at 14:57
  • @delueg It looks like you need to call `this.next()` inside of onRun now [see this](https://github.com/iron-meteor/iron-router#migrating-from-094). The error that you got could just be a case of bad messaging. If `this.next()` solves your issue I'll update the answer. – Neil Apr 28 '15 at 16:28
  • Hey, no this didn't solve my issue.. Doesn't change anything. I am not sure if i get it right.. Is it enough to just subscribe to a collection .. No initi.alisation needed?. And don't i have to have a correspondending publish method to a subscribe method? I posted my own question because maybe it is different ? http://stackoverflow.com/questions/29923423/meteor-iron-router-waiton-without-subscribe – Sven Delueg Apr 28 '15 at 18:43