Iron Router数据触发3次 [英] Iron Router data fires 3 times
问题描述
我在Router.Config上使用路由模板设置了此路由
I have this route set up with a loding template on Router.Config
Router.onBeforeAction('loading');
this.route('clients', {
path: '/clients',
template: 'clientsAll',
waitOn: function () {
return Meteor.subscribe('clientsAll');
},
data: function () {
if (this.ready()) {
console.log("Data");
return Clients.find().fetch();
}
}
});
一切正常,在呈现模板之前显示加载模板,但在日志上显示数据被触发两次.
Everything works fine it displays the loading template before rendering the template, but on the log it shows data being fired twice.
推荐答案
这是正常现象,像大多数路由方法一样,data
也在反应式计算中运行.
This is a normal behavior, data
like most route methods is run inside a reactive computation.
在您的data
方法中,您依赖于this.ready()
,它恰好是反应性数据源(它返回由waitOn
返回的等待列表的状态).
In your data
method you depend on this.ready()
which happens to be a reactive data source (it returns the state of the wait list returned by waitOn
).
所以基本上这就是发生的事情:
So basically this is what is going on :
- 您的数据方法被声明为一种计算,并且最初在
this.ready()
返回false的情况下运行. - 您等待的订阅将在将来的某个时间准备好,因此
this.ready()
现在返回true,并且您的data
方法将重新运行.
- your data method is declared as a computation and initially runs with
this.ready()
returning false. - the subscription you wait on becomes ready some time in the future so
this.ready()
now returns true and yourdata
method is rerun.
这不是问题,因为data
方法通常不占用大量计算资源(它们仅返回一些在客户端本地可用的数据).
This is not a problem because data
methods are usually not computational heavy (they just return some data that is available locally on the client).
这篇关于Iron Router数据触发3次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!