How to restart / render a meteor pattern after restarting another pattern?

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

eg.

Template.Nav.renderNav 

and inside this helper function I want to analyze the output from another helper in another template

For example, auxiliary

 Template.contentWindow.content 

which provides html for

 {{content}} 

and my renderNav helper wants to break up with html, which replaces {{content}} to generate html for

 {{renderNav}} 

how would i do that? right now, the {{renderNav}} helper is executing or running faster, and therefore it cannot 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 the renderNav helper is executed. (Which makes sense with what I see in terms of race conditions). Then the contentWindow displays and I hit the breakpoint above Session.set ('entryRendered', true). But then renderNav does not start again as you assume. Am I misinterpreting or incorrectly implementing your proposal?

+6
source share
2 answers

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

For example, you can set a reactive marker in the content template, which will notify renderNav what it did with the 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 the additional information you provided, we can create the following 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 ... } 
+4
source

If you want the navigation to be automatically rebuilt when the contentWindow displays as suggested by Hubert OG, you can also use a cleaner and lower level of context invalidation:

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

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

 var html = Template.contentWindow(); 

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

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

For detailed information on how this works, see screencasts at http://www.eventedmind.com/ about Sparks and reactivity.

UPDATE

To add an edited snippet to your DOM:

 document.body.appendChild(Meteor.render(function () { return '<h1>hello</h1><b>hello world</b>'; })); 

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

 console.log(reactiveFragment.childNodes[0]); 
+3
source

Source: https://habr.com/ru/post/949182/


All Articles