how to make a meteor template helper re-run/render after another template has rendered?

后端 未结 2 1064
日久生厌
日久生厌 2021-01-01 01:27

I have a template helper called {{renderNav}} in a template Nav

e.g.

Template.Nav.renderNav

and within that helper function I want

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-01 01:53

    You need a dependency in the template that you want to rerun. There are few possibilities, depending on what data you want to get.

    For example, you can set a reactive marker in the content template that will notify renderNav that it's done with drawing.

    Template.contentWidnow.rendered = function() {
        ...
    
        // Set this on the very end of rendered callback.
        Session.set('contentWindowRenderMark', '' +
            new Date().getTime() +
            Math.floor(Math.random() * 1000000) );
    }
    
    
    Template.renderNav.contentData = function() {
        // You don't have to actually use the mark value,
        // but you need to obtain it so that the dependency
        // is registered for this helper.
        var mark = Session.get('contentWindowRenderMark');
    
        // Get the data you need and prepare for displaying
        ...
    }
    

     


     

    With further information you've provided, we can create such code:

    content.js

    Content = {};
    Content._dep = new Deps.Dependency;
    

    contentWindow.js

    Template.contentWidnow.rendered = function() {
        Content.headers = this.findAll(':header');
        Content._dep.changed();
    }
    

    renderNav.js

    Template.renderNav.contentData = function() {
        Content._dep.depend();
        // use Content.headers here
        ...
    }
    

提交回复
热议问题