The strange thing is in Template.layout.onRendered way, if I go to /admin and return to /, the modals prepend, but if I go to /posts and return to /, nothing happens.
Maybe its something related to
My admin layout template is adminLayout.html and the front side layout template is layout.html, If the layout template changes, the layout.js will work? But my scroll up function can work anyway.
Oh in that case you can’t use layout.onRendered because it only runs once. You need to do it on the onRendered of the route templates because when you change route they will get re-run because they get destroyed when you go to a different route. The layout template doesn’t get destroyed unless you set a different layout somewhere else.
If you use the aldeed:template-extension package it’ll help you to attach the onRendered on a global level.
Not really answering your question, but you could use jchristman:reactive-modal, which is a library for reactive bootstrap modals… There are a couple of other options also so you don’t need to re-roll your own code.
Quick note about the link above - it is a fork of pahans:reactive-modal, which was not being maintained. I fixed all of the bugs I knew about and added a few reactive features.