Question> The way to run a function after onRendered for all templates
See original GitHub issueWhat 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:
- Created 3 years ago
- Comments:15 (8 by maintainers)
Top GitHub Comments
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-
Hey @kakadais,
Any progress on this one? Here’s a recent example on the similar use-case