npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

backbone-query-parameters

v0.4.0

Published

Backbone plugin which provides query parameter support for Backbone.Router

Downloads

408

Readme

Installation

Copy backbone.queryparams.js to your environment and include after backbone.js.

note: Backbone now supports minimal query parameters as of 1.1.1: http://backbonejs.org/#changelog

Query string route syntax

Any route except those ending with a wildcard will automatically accept additional content using the '?' separator. This content is a set of key value pairs using '&' as the pair separator and '=' as the key/value separator - just like the URL query string syntax.

If a query string exists in the route hash, the routing function (defined in the routes hash) will be given an additional parameter (in last ordinal position) containing hash of the key/value data.

routes: {
	'foo/:bar': 'myRoute'
}
...
myRoute: function(bar, params) {
	// the params attribute will be undefined unless there is a route containing a query string
}

Example route patterns

  • #foo/abc -> myRoute( 'abc', undefined )
  • #foo/abc?foo=bar -> myRoute( 'abc', {'foo': 'bar'} )

Named route parameters

All query parameters can be passed in a single hash using the key referenced from the route definition by setting Backbone.Router.namedParameters = true.

Using the previous example route patterns, the controller function would be

myRoute( {bar: 'abc', foo: 'bar'} );

Although this is not compatible with backbone, it is very useful to prevent router function parameter overloading where you have to always consider whether the route query parameters were passed.

Nested query strings

  • Any keys containing '.' will represent a nested structure.

  • Any keys containing '[]' will assume an array structure.

  • Any values containing '|' will assume an array structure.

  • Non-array values can still contain '|' but it must be URI encoded (%7C).

  • A prefix of '|' will ensure an array in case there is only a single value.

      #foo/abc?me.fname=Joe&me.lname=Hudson -> myRoute('abc', {'me': {'fname': 'Joe', 'lname': 'Hudson'}} )
    
      #foo/abc?animals=cat|dog -> myRoute( 'abc', ['cat', 'dog'] )
    
      #foo/abc?animals=|cat -> myRoute( 'abc', ['cat'] )

Generating route with query string

You can create a route string from a route + parameter hash using the router.toFragment(route, parameters) method. It can contain a nested hash structure or arrays. ex:

var route = router.toFragment('myroute', {
	a:'l', b:{c: 'n', d:'m', e:{f: 'o'}}, array1:['p'], array2:['q', 'r'], array3:['s','t','|']
});

Regular Expression Routes

If you want to use this plugin with regex routes you'll need to append the query capture component (([\?]{1}.*)?) manually.

router.route(/foo\/([^\/]+)\//, 'foo:event', callback)

should be written

router.route(/foo\/([^\/]+)\/([\?]{1}.*)?/, 'foo:event', callback)

Supported Versions

This library supports Backbone 1.0.0 and 1.1.0. Backbone 1.1.0 users need to either include the backbone.queryparams-1.1-shim.js file or modify their backbone libray to remove this line from Backbone.History.prototype.navigate.

      // Strip the fragment of the query and hash for matching.
      fragment = fragment.replace(pathStripper, '');

This library can also be used when Marionette 1.2.2 (with router/appRoutes) is used on top of Backbone.