呈现另一个模板渲染后

呈现另一个模板渲染后

本文介绍了如何使流星模板助手重新运行/呈现另一个模板渲染后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在模板Nav中有一个名为{{renderNav}}的模板助手。例如

   c $ c>模板,它会通知 renderNav 它完成绘图。

  Template.contentWidnow.rendered = function(){
...

//在渲染回调的最后设置它。
Session.set('contentWindowRenderMark',''+
new Date()。getTime()+
Math.floor(Math.random()* 1000000));
}


Template.renderNav.contentData = function(){
//您不必实际使用标记值
/ /但您需要获取它,以便为此帮助程序注册依赖项
//。
var mark = Session.get('contentWindowRenderMark');

//获取您需要的数据并准备显示
...
}

 






 

$ b

有了您提供的进一步信息,我们可以创建此类代码: content.js


$ b

  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();
//在这里使用Content.headers
...
}


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

e.g.

Template.Nav.renderNav

and within that helper function I want to parse the rendered output of another helper within a different template

For example the helper

Template.contentWindow.content

which provides the html for

{{content}}

and my renderNav helper wants to part the html that replaces {{content}} to generate the html for

{{renderNav}}

how would I do this? right now the {{renderNav}} helper executes for or runs more quickly and so it is unable to parse the html that replaces {{content}}

@Hugo - I did the following in my code as you suggested

Template.contentWindow.rendered = function() {
    debugger;
    return Session.set('entryRendered', true);
};

Template.Nav.renderNav = function() {
    debugger;
    var forceDependency;
    return forceDependency = Session.get('entryRendered');
};

When I run it, the debugger first stops when executing the renderNav helper. (Which makes sense with what I am seeing in terms of the race condition). Then contentWindow renders and I hit the breakpoint above the Session.set('entryRendered', true). But then the renderNav doesn't run again as you suggest it should. Did I misinterpret or incorrectly implement your suggestion?

解决方案

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
    ...
}

这篇关于如何使流星模板助手重新运行/呈现另一个模板渲染后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 23:12