Iron Router data fires 3 times

隐身守侯 提交于 2019-12-10 10:37:10

问题


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 your data 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!