Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 具有异步功能的Meteor iron路由器waitOn_Javascript_Meteor_Iron Router - Fatal编程技术网

Javascript 具有异步功能的Meteor iron路由器waitOn

Javascript 具有异步功能的Meteor iron路由器waitOn,javascript,meteor,iron-router,Javascript,Meteor,Iron Router,我需要在异步函数准备好后返回iron router的waitOn参数中的subscribtion 它应该是这样工作的: this.route('myRoute', { template : 'myTemplate', path : '/foo/:param', waitOn : function () { MyAsyncFunction(function(this.params.param){ var result = 'whate

我需要在异步函数准备好后返回iron router的waitOn参数中的subscribtion

它应该是这样工作的:

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()
    }
});

用熨斗路由器解决这个问题的最好方法是什么?我正在寻找一个推荐的解决方案。

您可以从
onRun
挂钩运行
MyAsyncFunction
,并使用它设置一个反应变量,该变量将触发订阅更新

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()
    }
});

当参数更改时,
onRun
钩子将重新运行。

“在第一次加载路由时只运行一次。请注意,如果您的页面通过热代码重新加载重新加载,则不会再次运行此钩子,因此请确保您设置的任何变量将在HCR上保持不变(例如会话变量)。”这似乎不是正确的方式,不是吗?它不需要加载一次,每次都要加载,我将重新加载路由或使用另一个参数,但使用同一个参数时也要加载。@TimoRütten刚刚更新到最新的Iron Router,它不支持加载,而支持onRun。它将在参数更改或页面刷新上运行,但不会在HCR上运行。感谢提供此信息。我明天会试试这个,然后给你回复。它看起来像您描述的那样工作,但问题是-我真的不知道为什么-此回调将被调用2次,而不是每次加载或参数更改调用一次。嘿,我收到错误“路由调度从未呈现。您是否忘记在onBeforeAction中调用此.next()但是我没有在这个路由中定义onBeforeAction函数。尼尔:你能帮忙吗