流星铁路由器等待与异步功能

Meteor iron-router waitOn with async function

本文关键字:异步 功能 等待 路由器 流星      更新时间:2023-09-26

在异步函数准备好后,我需要在铁路由器的waitOn参数中返回我的订阅。

应该是这样的:

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钩子将重新运行。