问题
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.
回答1:
This is a normal behavior, data
like most route methods is run inside a reactive computation.
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 :
- 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.
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).
来源:https://stackoverflow.com/questions/25432476/iron-router-data-fires-3-times