Added a managed pop state listener to the route extension so that back events are handled correctly.

This commit is contained in:
Matt Mo 2021-11-11 11:43:10 -08:00
parent 271e6e41fa
commit 6701c19956

View File

@ -55,16 +55,22 @@ IgniteTemplate.prototype.route = function(routes, showCallback = null, hideCallb
} }
}; };
//Create a managed callback //Create a managed push & pop callback
var managed = new IgniteCallback(update, () => { var managedPush = new IgniteCallback(update, () => {
window.removeEventListener("pushstate", managed.callback); window.removeEventListener("pushstate", managedPush.callback);
}); });
//Register our callback to the route event. var managedPop = new IgniteCallback(update, () => {
window.addEventListener("pushstate", managed.callback); window.removeEventListener("popstate", managedPop.callback);
});
//Add the managed callback to our template so that upon deconstruction our callback is destroyed correctly. //Register our push & pop callbacks.
this._callbacks.push(managed); window.addEventListener("pushstate", managedPush.callback);
window.addEventListener("popstate", managedPop.callback);
//Add the managed callbacks to our template so that upon deconstruction our callback is destroyed correctly.
this._callbacks.push(managedPush);
this._callbacks.push(managedPop);
//Create a constructor callback that will update the state upon first load. //Create a constructor callback that will update the state upon first load.
this._constructors.push(update); this._constructors.push(update);
@ -294,6 +300,11 @@ class Router {
Router.states.pop(); //Pop the current state. Router.states.pop(); //Pop the current state.
Router.navigate(Router.states.pop(), null, refresh); //Navigate to the previous state. Router.navigate(Router.states.pop(), null, refresh); //Navigate to the previous state.
} else { } else {
//Pop the current state, otherwise it can be used to go back but we don't want that.
if (Router.states.length > 0) {
Router.states.pop();
}
if (fallback) { if (fallback) {
Router.navigate(fallback, null, refresh); Router.navigate(fallback, null, refresh);
} else { } else {