Skip to content

Commit

Permalink
Merge pull request #59 from swipely/fix_query_string_hashroute
Browse files Browse the repository at this point in the history
Dont treat query strings as part of a route
  • Loading branch information
barnabyc committed May 16, 2014
2 parents bf0d8df + cafb22d commit ce615db
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 21 deletions.
27 changes: 19 additions & 8 deletions aviator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('#', '');
Expand All @@ -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;
}
}
},
Expand All @@ -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);
Expand Down
62 changes: 57 additions & 5 deletions spec/navigator_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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' );
});
});
});
Expand All @@ -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' );
});
});
});
Expand Down Expand Up @@ -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 () {
Expand Down
2 changes: 1 addition & 1 deletion spec/route_spec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
describe('Route', function () {

var routes, uri, subject, usersTarget;
var routes, uri, subject, usersTarget, navigator;

beforeEach(function () {
usersTarget = {
Expand Down
25 changes: 18 additions & 7 deletions src/navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('#', '');
Expand All @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit ce615db

Please sign in to comment.