From f1cfbd952dd46f5e270fe887e73be2a19f26484b Mon Sep 17 00:00:00 2001 From: Bart Flaherty Date: Tue, 22 Dec 2015 16:30:33 -0500 Subject: [PATCH] Allow specification of never using hash routes Some folks might not want to use hash routes, even if the browser doesn't support push state. This adds an option to fallback to full page refreshes upon calls to `navigate`. --- aviator.js | 32 ++++++++++++++++++++++++-------- src/main.js | 14 +++++++++++--- src/navigator.js | 18 +++++++++++++----- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/aviator.js b/aviator.js index 8936011..46f7f70 100644 --- a/aviator.js +++ b/aviator.js @@ -135,6 +135,13 @@ var Aviator = { **/ root: '', + /** + @property fallbackToHashRoutes + @type {Boolean} + @default true + **/ + fallbackToHashRoutes: true, + /** @property _navigator @type {Navigator} @@ -160,9 +167,10 @@ var Aviator = { var navigator = this._navigator; navigator.setup({ - pushStateEnabled: this.pushStateEnabled, - linkSelector: this.linkSelector, - root: this.root + fallbackToHashRoutes: this.fallbackToHashRoutes, + pushStateEnabled: this.pushStateEnabled, + linkSelector: this.linkSelector, + root: this.root }); navigator.dispatch(); @@ -285,7 +293,11 @@ Navigator.prototype = { } } - this._attachEvents(); + // If not using push state or hash routes, there's no + // need for Aviator to listen to any events. + if (this.pushStateEnabled || this.fallbackToHashRoutes) { + this._attachEvents(); + } }, /** @@ -335,7 +347,7 @@ Navigator.prototype = { @return {String} **/ getCurrentPathname: function () { - if (this.pushStateEnabled) { + if (this.pushStateEnabled || !this.fallbackToHashRoutes) { return this._removeURIRoot(location.pathname); } else { @@ -348,7 +360,7 @@ Navigator.prototype = { @return {String} **/ getCurrentURI: function () { - if (this.pushStateEnabled) { + if (this.pushStateEnabled || !this.fallbackToHashRoutes) { return this._removeURIRoot(location.pathname) + location.search; } else { @@ -363,7 +375,7 @@ Navigator.prototype = { getQueryString: function () { var uri, queryString; - if (this.pushStateEnabled) { + if (this.pushStateEnabled || !this.fallbackToHashRoutes) { return location.search || null; } else { @@ -494,10 +506,14 @@ Navigator.prototype = { this.onURIChange(); } - else { + else if (this.fallbackToHashRoutes) { if (options.replace) location.replace('#' + link); else location.hash = link; } + else { + location.replace(this.root + link); + return; + } }, /** diff --git a/src/main.js b/src/main.js index 1eba887..ab03656 100644 --- a/src/main.js +++ b/src/main.js @@ -32,6 +32,13 @@ var Aviator = { **/ root: '', + /** + @property fallbackToHashRoutes + @type {Boolean} + @default true + **/ + fallbackToHashRoutes: true, + /** @property _navigator @type {Navigator} @@ -57,9 +64,10 @@ var Aviator = { var navigator = this._navigator; navigator.setup({ - pushStateEnabled: this.pushStateEnabled, - linkSelector: this.linkSelector, - root: this.root + fallbackToHashRoutes: this.fallbackToHashRoutes, + pushStateEnabled: this.pushStateEnabled, + linkSelector: this.linkSelector, + root: this.root }); navigator.dispatch(); diff --git a/src/navigator.js b/src/navigator.js index 8f9523e..fb39365 100644 --- a/src/navigator.js +++ b/src/navigator.js @@ -34,7 +34,11 @@ Navigator.prototype = { } } - this._attachEvents(); + // If not using push state or hash routes, there's no + // need for Aviator to listen to any events. + if (this.pushStateEnabled || this.fallbackToHashRoutes) { + this._attachEvents(); + } }, /** @@ -84,7 +88,7 @@ Navigator.prototype = { @return {String} **/ getCurrentPathname: function () { - if (this.pushStateEnabled) { + if (this.pushStateEnabled || !this.fallbackToHashRoutes) { return this._removeURIRoot(location.pathname); } else { @@ -97,7 +101,7 @@ Navigator.prototype = { @return {String} **/ getCurrentURI: function () { - if (this.pushStateEnabled) { + if (this.pushStateEnabled || !this.fallbackToHashRoutes) { return this._removeURIRoot(location.pathname) + location.search; } else { @@ -112,7 +116,7 @@ Navigator.prototype = { getQueryString: function () { var uri, queryString; - if (this.pushStateEnabled) { + if (this.pushStateEnabled || !this.fallbackToHashRoutes) { return location.search || null; } else { @@ -243,10 +247,14 @@ Navigator.prototype = { this.onURIChange(); } - else { + else if (this.fallbackToHashRoutes) { if (options.replace) location.replace('#' + link); else location.hash = link; } + else { + location.replace(this.root + link); + return; + } }, /**