Skip to content

Commit

Permalink
Merge pull request #56 from swipely/fix_broken_query_string_with_hash…
Browse files Browse the repository at this point in the history
…_routing

Fix broken query string parsing with hash routing
  • Loading branch information
barnabyc committed May 16, 2014
2 parents 19306ef + 7877872 commit bf0d8df
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
23 changes: 20 additions & 3 deletions aviator.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,23 @@ Navigator.prototype = {
@return {String|Null}
**/
getQueryString: function () {
return location.search || null;
var uri, queryString;

if (this.pushStateEnabled) {
return location.search || null;
}
else {
uri = this.getCurrentURI();

if (uri.indexOf('?') !== -1) {
queryString = uri.split('?')[1];

return queryString ? '?' + queryString : null;
}
else {
return null
}
}
},

/**
Expand Down Expand Up @@ -426,8 +442,9 @@ Navigator.prototype = {
@param {Object} [options]
**/
navigate: function (uri, options) {
var options = options || {},
namedParams = options.namedParams,
options = options || {};

var namedParams = options.namedParams,
queryParams = options.queryParams;

// halt any previous action invocations
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"grunt-browserify": "~1.2.0"
},
"scripts": {
"test": "./node_modules/grunt-cli/bin/grunt jasmine --verbose"
"test": "./node_modules/grunt-cli/bin/grunt test"
},
"engine": {
"node": ">=0.8.x"
Expand Down
38 changes: 38 additions & 0 deletions spec/navigator_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,44 @@ describe('Navigator', function () {
window.history.replaceState({}, '', '/_SpecRunner.html');
});

describe('#getQueryString', function () {
describe('with push state disabled', function () {
beforeEach(function () {
subject.pushStateEnabled = false;
});

describe('and a query string in the current uri', function () {
beforeEach(function () {
spyOn( subject, 'getCurrentURI' ).andReturn( 'foo/bar?baz=123' );
});

it('gets the queryString from the hash url', function () {
expect( subject.getQueryString() ).toBe( '?baz=123' );
});
});

describe('and a no query string in the current uri', function () {
beforeEach(function () {
spyOn( subject, 'getCurrentURI' ).andReturn( 'foo/bar' );
});

it('gets the queryString from the hash url', function () {
expect( subject.getQueryString() ).toBe( null );
});
});

describe('and an empty query string in the current uri', function () {
beforeEach(function () {
spyOn( subject, 'getCurrentURI' ).andReturn( 'foo/bar?' );
});

it('gets the queryString from the hash url', function () {
expect( subject.getQueryString() ).toBe( null );
});
});
});
});

describe('#getCurrentURI', function () {
var root;

Expand Down
18 changes: 17 additions & 1 deletion src/navigator.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,23 @@ Navigator.prototype = {
@return {String|Null}
**/
getQueryString: function () {
return location.search || null;
var uri, queryString;

if (this.pushStateEnabled) {
return location.search || null;
}
else {
uri = this.getCurrentURI();

if (uri.indexOf('?') !== -1) {
queryString = uri.split('?')[1];

return (queryString ? '?' + queryString : null);
}
else {
return null;
}
}
},

/**
Expand Down

0 comments on commit bf0d8df

Please sign in to comment.