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

后端 未结 2 1066
日久生厌
日久生厌 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条回答
  •  梦毁少年i
    2021-01-01 01:50

    If you want the navigation to be automatically rebuilt when contentWindow renders, as Hubert OG suggested, you can also use a cleaner, lower level way of invalidating contexts:

    var navDep = new Deps.Dependency;
    
    Template.contentWindow.rendered = function() {
        ...
        navDep.changed();
    }
    
    Template.renderNav.contentData = function() {
        navDep.depend();
    
        // Get the data you need and prepare for displaying
        ...
    }
    

    See http://docs.meteor.com/#deps for more info.

    If, on the other hand, you want to render another template manually, you can call it as a function:

    var html = Template.contentWindow();
    

    The returned html will not be reactive. If you need reactivity, use:

    var reactiveFragment = Meteor.render(Template.contentWindow);
    

    See the screencasts at http://www.eventedmind.com/ on Spark and reactivity for details on how this works.

    UPDATE

    To add a rendered fragment to your DOM:

    document.body.appendChild(Meteor.render(function () {
        return '

    hello

    hello world'; }));

    You can also access the rendered nodes directly using the DOM API:

    console.log(reactiveFragment.childNodes[0]);
    

提交回复
热议问题