question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Question> The way to run a function after onRendered for all templates

See original GitHub issue

What I want

Run a function always after all templates onRendered with FlowRouter way. (not the way of jQuery or direct dom control)

Ex> In the case of dynamic templete with blaze-layout, we could have a lot of nested templates.

<template name="layout">
    {{> header}}
    {{> Template.dynamic template=name}} # or {{> yield}}
    {{> footer}}
</template>

And I need to run some code foo() once to control Dom after rendering all, always. I could accomplish this run the foo() in all of the dynamic templates, but seems like a bad idea.

  • The foo() is simple jQuery Dom handling for some reason.

Additional Example to explain

Suppose that I have five 5 templates and each template has the route by different url path, so if a user clicks the link and changes the path, the dynamic template just rendering on its position in the layout.

In this case, I have to add the foo() to the 5 templates this.render() in their callback to run the foo() every single time after rendering.

If I add the foo()to ‘layout’ template’s this.render() in callback, it is working at the first rendering of ‘layout’ template, but the foo() is not running on the next path changing by user’s click action. (dynamic template is changing but the layout’s this.render() is not working cause it’s already been rendered before)

So what I want to do is, run the foo() every single time when the dynamic path changing, without adding the foo() to all callback in the dynamic template’s this.render().

Declare once and run everytime when the path chaning after rendering to avoid code mess and repeats

Codes Example

FlowRouter.route '/notice',
  name: 'notice'
  action: ->
    @render 'layout', 'notice', ->
      foo()

FlowRouter.route '/news',
  name: 'news'
  action: ->
    @render 'layout', 'news', ->
      foo()      

I just want to do the same thing without foo() repeats.

FlowRouter.route '/layout',
  name: 'layout'
  action: ->
    @render 'layout', ->
      foo()

This is not working on dynamic template changing.

watchPathChange way

https://github.com/VeliovGroup/flow-router/blob/master/docs/api/watchPathChange.md

This was the best approach in a document that I found. but I think there would be a better way to do this with the flow-router way.

Template.layout.onRendered ->
  console.log 'layout'

Template.something.onRendered ->
  console.log 'something'

FlowRouter.triggers.enter [-> console.log 'triggers']

Tracker.autorun ->
  console.log 'autorun'
  FlowRouter.watchPathChange()
  foo()

Result of on first load or full reload the page

autorun triggers autorun something layout

The foo() in ‘autorun’ fired faster than ‘something’, and ‘triggers’ is even faster.

Result of on changing path

triggers autorun publication

The foo() is faster still. But, in this case, the dom control that I want is worked. (Actually this part is more tricky for me.) Anyway, I think that this is not the best idea.

So What I want Again

is run the foo() after the last template rendered.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
kakadaiscommented, Apr 22, 2021

I’ve run a function everytime when its needed such as your proposal. I still have some curious there would be a better way to apply this, but couldn’t make it. The case was so specific for my case, so this would be better to be closed. Thanks-

0reactions
dr-dimitrucommented, Apr 21, 2021

Hey @kakadais,

Any progress on this one? Here’s a recent example on the similar use-case

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to trigger function after render template - Stack Overflow
It's resolved, I have to use onShow instead of onRender function. Now it's working fine.
Read more >
Why a function executed onRendered work or not - help
Hi,. I create a navbar… with a menu which is loaded with templates and helpers. I start to add some styles and hide/show...
Read more >
Parent onRendered triggers before child's #4410 - GitHub
onRendered is called after editContact.onRendered (the parent). The problem is that Tracker.afterFlush sets functions to run in the next ...
Read more >
Blaze | BlazeJS
Use Blaze.remove to remove a template or View previously inserted with Blaze.render , in such a way that any behaviors attached to the...
Read more >
Run any other javascript after the Display Templates have ...
Is there a way to add the post render function from outside the template? For example I have a code snippet on the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found