meteorjs iron-router waitOn and using as data on rendered

后端 未结 4 683
长情又很酷
长情又很酷 2020-12-14 22:46

I try to get the returned data in my Template.rendered function.

The current code is:

this.route(\'editCat\', {
    layoutTemplate : \'l         


        
4条回答
  •  不思量自难忘°
    2020-12-14 23:28

    Like @Sean said, the right solution is to use a loading template:

    Router.onBeforeAction("loading");
    

    But if you don't want it, like me, I came up with this solution:

    Template.xxx.rendered = function() {
        var self = this;
    
        this.autorun(function(a) {
            var data = Template.currentData(self.view);
            if(!data) return;
    
            console.log("has data! do stuff...");
            console.dir(data);
            //...
        });
    }
    

    Template.currentData is reactive, so in the first time it is null and in the second it has your data.

    Hope it helps.

    -- Tested on Meteor v1.0 with Iron Router v1.0

提交回复
热议问题