Skip to content

Instantly share code, notes, and snippets.

@pkarl
Created February 11, 2013 23:03
Show Gist options
  • Save pkarl/4758430 to your computer and use it in GitHub Desktop.
Save pkarl/4758430 to your computer and use it in GitHub Desktop.

This PR adds support for URL query string parameters to pathjs. While query string params are typically used to send information to an application running on a server, I ran into a use case where query string params would be very useful in the construction of a single page app.

Specifically, I wanted to give other technical teams (and myself) the ability to modify certain app-wide parameters through a simple query string key/value pair.

This PR add support for the following situations, and has the following effects:

// if this were the pathjs mapping...
pathjs.map('#/some/:id').to(function() {
  console.log(this.params);
});

pathjs.rescue(function(path) {
  console.log("RESCUE");
});

// we'd see the following URL -> console output

// -- http://somesite.com/#/some/10?debug
{ 'id': 10, 'debug': true }

// -- http://somesite.com/#/some/10?
"RESCUE"

// -- http://somesite.com/#/some/10?debug=false
{ 'id': 10, 'debug': false }

// -- http://somesite.com/#/some/10?debug&id=20
{ 'id': 10, 'debug': true }

// -- http://somesite.com/#/some/10?id
{ 'id': 10 }

// -- http://somesite.com/#/some/10?debug=false&debug
{ 'id': 10, 'debug': true }
@str
Copy link

str commented May 29, 2017

Why should #/some/10? trigger rescue? it's just an empty query string, so it's should be the same as #/some/10

@str
Copy link

str commented May 29, 2017

Also, what about arrays? Should they work as in PHP?

#/some/10?notify=[foo@example.com,bar@example.com]&from=[name='dev #1', email=dev@example.com]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment