diff --git a/ignite-html-router.js b/ignite-html-router.js index ae6eea9..636f601 100644 --- a/ignite-html-router.js +++ b/ignite-html-router.js @@ -46,6 +46,9 @@ IgniteTemplate.prototype.route = function (routes, showCallback = null, hideCall //Invoke the callback if the route matched. if (routeMatches) { + //Determine if we are going back to this route. + var back = (event && event.type == "popstate"); + //If the route is already visible, call hidden, because we are reshowing the route. if (hideCallback && this.element.style.display != "none") { hideCallback(); @@ -55,7 +58,7 @@ IgniteTemplate.prototype.route = function (routes, showCallback = null, hideCall this.element.style.removeProperty("display"); if (showCallback) { - showCallback(matchedRoute, (event && event.data ? event.data : data)); + showCallback(matchedRoute, (event && event.data ? event.data : data), back); } } else { //Hide the route element. @@ -293,8 +296,9 @@ class Router { * @param {Any} data The data to pass along with this navigate, this only applies when not refreshing, default is null. * @param {Boolean} refresh Whether or not to refresh the page, default is false. * @param {Boolean} keepQuery Whether or not to keep the current url query, default is false. + * @param {Boolean} back Whether or not we are navigating back to this route, default is false. */ - static navigate(route, data = null, refresh = false, keepQuery = false) { + static navigate(route, data = null, refresh = false, keepQuery = false, back = false) { //If keepQuery is true then include the current query. if (keepQuery) { if (route.includes("?")) { @@ -342,10 +346,16 @@ class Router { //Push the route to our internal states Router.states.push(route); - //Fire a pushstate event to make the routes switch. - var event = new Event("pushstate"); - event.data = data; - window.dispatchEvent(event); + //Fire a pushstate or popstate event to make the routes switch. + if (back) { + var event = new Event("popstate"); + event.data = data; + window.dispatchEvent(event); + } else { + var event = new Event("pushstate"); + event.data = data; + window.dispatchEvent(event); + } } } @@ -359,8 +369,8 @@ class Router { //Pop the current state. Router.states.pop(); - //Navigate to the previous state - Router.navigate(Router.states.pop(), null, refresh, false); + //Navigate to the previous state (Back is true, because we are going to a previous state) + Router.navigate(Router.states.pop(), null, refresh, false, true); } else { //Pop the current state, otherwise it can be used to go back but we don't want that. if (Router.states.length > 0) { @@ -368,7 +378,7 @@ class Router { } if (fallback) { - Router.navigate(fallback, null, refresh); + Router.navigate(fallback, null, refresh, false, false); } else { window.history.back(); }