Hi, I am experiencing a problem while using the router’s .back() function. When I call back it properly returns to the previous page in the history but it then runs all of the JS on that page more than once. Let’s say I am on page 1, navigate forwards to page 2, and the forwards to page 3. From page 3 I go back to page 2. The JS on page 2 runs twice. If I go back to page 1 the JS runs 3 times. By “runs” I mean things like: handlers get attached multiple times, when handler code is executed it runs mulitple times, etc.
I have one index.js file which contains some common code and sets things like the Framework7 material (router and initializes app, etc.) and then a JS page with code specifically for each other page of my application. SO, my JS directory looks like this:
index.js
page1.js
page2.js
…
If I am on page 2, I only want the “common” JS and the page specific JS to execute. So, for page 2 that would be index.js and page2.js. I don’t want to worry about code for page 1 or page N running when I am not on those pages. I could put all the JS in one file but that would be too messy and doesn’t solve what I believe the problem actually is.
I have not built single page applications before, so this is a little new to me. I am not really certain on what the standard is when it comes to how you load JS and CSS files for each page in a SPA. I have tried the following to no avail:
I put the CSS and JS files in the page element:
index.html:
<body>
<div ip="appMain">
<div class="view view-main">
</div>
</div>
...End HTML
Other scripts like the framework7.min.js, etc.
<script src="js/index.js"></script>
</body>
page1.html:
<div class="page" data-name="login">
...HTML
<script src="js/page1.js"></script>
</div>
This doesn’t work as none of the code is executed in page1.js.
I have ended up having to do this:
index.html:
<body>
<div id="appMain">
<div class="view view-main">
</div>
</div>
...End HTML
All script files are listed here
<script src="js/index.js"></script>
<script src="js/page1.js"></script>
</body>
page1.js:
fw7.on("pageBeforeIn", function() {
if(mainView.router.currentRoute.name === "login") {
//All of the code for page1 goes here
}
});
This works but I have a very strong feeling the error I am experiencing is due to the “pageBeforeIn” being triggered after/during the back navigations because the page is in the history. Thinking this I tried:
index.html:
<body>
<div ip="appMain">
<div class="view view-main">
</div>
</div>
...End HTML
Other scripts like the framework7.min.js, etc.
<script src="js/index.js"></script>
</body>
page1.html:
<div class="page" data-name="login">
...HTML
<script src="js/page1.js"></script>
</div>
page1.js:
if(mainView.router.currentRoute.name === "login") {
//All of the code for page1 goes here
}
However, this doesn’t work either.
I have tried using other page events but they don’t work every time you end up on the page and I don’t really think it is correct to check for a whole bunch of different page events to determine when to trigger the page code. It seems that the .back() issues are due to how I am including and executing the JS files. What is the proper way for me to do this?
Sorry this is a long post, but I would prefer to be more detailed up-front.
Thanks!