diff --git a/aviator.js b/aviator.js index 23ab746..b15f3eb 100644 --- a/aviator.js +++ b/aviator.js @@ -315,13 +315,26 @@ Navigator.prototype = { return this._request; }, + /** + @method getCurrentPathname + @return {String} + **/ + getCurrentPathname: function () { + if (this.pushStateEnabled) { + return this._removeURIRoot(location.pathname); + } + else { + return location.hash.replace('#', '').split('?')[0]; + } + }, + /** @method getCurrentURI @return {String} **/ getCurrentURI: function () { if (this.pushStateEnabled) { - return this._removeURIRoot(location.pathname); + return this._removeURIRoot(location.pathname) + location.search; } else { return location.hash.replace('#', ''); @@ -339,15 +352,13 @@ Navigator.prototype = { return location.search || null; } else { - uri = this.getCurrentURI(); - - if (uri.indexOf('?') !== -1) { - queryString = uri.split('?')[1]; + queryString = this.getCurrentURI().split('?')[1]; - return queryString ? '?' + queryString : null; + if (queryString) { + return '?' + queryString; } else { - return null + return null; } } }, @@ -356,7 +367,7 @@ Navigator.prototype = { @method dispatch **/ dispatch: function () { - var uri = this.getCurrentURI(), + var uri = this.getCurrentPathname(), route = this.createRouteForURI(uri), queryString = this.getQueryString(), request = this.createRequest(uri, queryString, route.matchedRoute); diff --git a/spec/navigator_spec.js b/spec/navigator_spec.js index 5fbe1a1..9d78977 100644 --- a/spec/navigator_spec.js +++ b/spec/navigator_spec.js @@ -57,6 +57,48 @@ describe('Navigator', function () { }); }); + describe('#getCurrentPathname', function () { + var root; + + beforeEach(function () { + root = '/_SpecRunner.html'; + + subject.root = root; + }); + + describe('with push state enabled', function () { + beforeEach(function () { + subject.pushStateEnabled = true; + }); + + describe('and the uri is /foo/bar', function () { + beforeEach(function () { + window.history.pushState({}, '', root + '/foo/bar?bar=123'); + }); + + it('returns /foo/bar', function () { + expect( subject.getCurrentPathname() ).toBe( '/foo/bar' ); + }); + }); + }); + + describe('with push state disabled', function () { + beforeEach(function () { + subject.pushStateEnabled = false; + }); + + describe('and the uri is #/foo/bar', function () { + beforeEach(function () { + window.location.hash = '/foo/bar?bar=123'; + }); + + it('returns /foo/bar', function () { + expect( subject.getCurrentPathname() ).toBe( '/foo/bar' ); + }); + }); + }); + }); + describe('#getCurrentURI', function () { var root; @@ -73,11 +115,11 @@ describe('Navigator', function () { describe('and the uri is /foo/bar', function () { beforeEach(function () { - window.history.pushState({}, '', root + '/foo/bar'); + window.history.pushState({}, '', root + '/foo/bar?bar=123'); }); it('returns /foo/bar', function () { - expect( subject.getCurrentURI() ).toBe( '/foo/bar' ); + expect( subject.getCurrentURI() ).toBe( '/foo/bar?bar=123' ); }); }); }); @@ -89,11 +131,11 @@ describe('Navigator', function () { describe('and the uri is #/foo/bar', function () { beforeEach(function () { - window.location.hash = '/foo/bar'; + window.location.hash = '/foo/bar?bar=123'; }); it('returns /foo/bar', function () { - expect( subject.getCurrentURI() ).toBe( '/foo/bar' ); + expect( subject.getCurrentURI() ).toBe( '/foo/bar?bar=123' ); }); }); }); @@ -308,12 +350,22 @@ describe('Navigator', function () { describe('with push state disabled', function () { beforeEach(function () { subject.pushStateEnabled = false; - subject.navigate('/foo/bar'); }); it('changes the hash to the href', function () { + subject.navigate('/foo/bar'); expect( window.location.hash ).toBe( '#/foo/bar' ); }); + + describe('and navigating with a queryString', function () { + beforeEach(function () { + subject.navigate('/foo/bar?baz=123'); + }); + + it('changes the hash to the href with a queryString', function () { + expect( window.location.hash ).toBe( '#/foo/bar?baz=123' ); + }); + }); }); describe('when called with silent: true', function () { diff --git a/spec/route_spec.js b/spec/route_spec.js index 1bc98c1..5dc5d0a 100644 --- a/spec/route_spec.js +++ b/spec/route_spec.js @@ -1,6 +1,6 @@ describe('Route', function () { - var routes, uri, subject, usersTarget; + var routes, uri, subject, usersTarget, navigator; beforeEach(function () { usersTarget = { diff --git a/src/navigator.js b/src/navigator.js index a072633..e62defa 100644 --- a/src/navigator.js +++ b/src/navigator.js @@ -79,13 +79,26 @@ Navigator.prototype = { return this._request; }, + /** + @method getCurrentPathname + @return {String} + **/ + getCurrentPathname: function () { + if (this.pushStateEnabled) { + return this._removeURIRoot(location.pathname); + } + else { + return location.hash.replace('#', '').split('?')[0]; + } + }, + /** @method getCurrentURI @return {String} **/ getCurrentURI: function () { if (this.pushStateEnabled) { - return this._removeURIRoot(location.pathname); + return this._removeURIRoot(location.pathname) + location.search; } else { return location.hash.replace('#', ''); @@ -103,12 +116,10 @@ Navigator.prototype = { return location.search || null; } else { - uri = this.getCurrentURI(); - - if (uri.indexOf('?') !== -1) { - queryString = uri.split('?')[1]; + queryString = this.getCurrentURI().split('?')[1]; - return (queryString ? '?' + queryString : null); + if (queryString) { + return '?' + queryString; } else { return null; @@ -120,7 +131,7 @@ Navigator.prototype = { @method dispatch **/ dispatch: function () { - var uri = this.getCurrentURI(), + var uri = this.getCurrentPathname(), route = this.createRouteForURI(uri), queryString = this.getQueryString(), request = this.createRequest(uri, queryString, route.matchedRoute);