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.base-router

v1.3.2

Published

A better starting point for a new Backbone Router.

Downloads

22

Readme

backbone.base-router

A better starting point for creating your own routing abstractions in Backbone.

What problems does this library solve?

Backbone's Router has two frustrating properties: it is too simple and too difficult to change. This library solves the second problem to make it easier for you to solve the first problem.

Instead of requiring that you associate a callback with a route, this library lets you associate whatever you'd like with a route. It can be a callback if you want, but it can also be an object, or even a string.

Whenever a Route is matched, a single method on the Router is called. This method is passed a single argument, routeData, that contains as much about the matched route as possible. Included this object are parsed query parameters, named fragment params, and the object you associated with the route, among other things. This single point-of-entry, combined with all of this data, makes it remarkably easy to add new abstractions to the Router.

What problems doesn't this library solve?

This library is not an effort to rewrite Backbone.history. As such, some of History's quirks are carried over, too. For instance, the order that you specify your callbacks in still matters, as this is how Backbone.History matches routes.

Getting Started

The Single Point of Entry

The API for the Base Router is simple: there's a single callback that gets called when a Route is navigated to. This callback is a plethora of information you might need, such as parsed query parameters and whatever object was associated with the callback. This is the location where you build your abstractions from.

Removed features

Backbone.BaseRouter does more for you by doing less. The following features were removed from the router.

  • A callback, if specified, is not automatically executed
  • No routing-related events are fired

The point of removing these features is that it gives you complete control over the Routing mechanism. It's simple to add them back in. Or you can change them to be exactly how you want. Or just leave them out. It's entirely up to you.

Example Usage

See the examples/ directory. There are READMEs for each example.

API

onNavigate( routeData )

The single point of entry is the onNavigate method. This method is called each time the user navigates via Backbone.history.

// Create a new Base Router
var router = new BaseRouter();

// Each time the user navigates to a matched route, a console message
// logs all of the data passed to the callback.
router.onNavigate = function(routeData) {
  console.log('The user has navigated!', routeData);
};

In addition to being called everytime that the user navigates to a matched route, which in itself is useful, the callback is passed a plethora of useful data related to the navigation action. This information is contained in the routeData argument.

routeData

linked

The object that was associated with this route. In a traditional Backbone router, this is always a callback that is executed. In the BaseRouter, this can be anything, and no assumptions are made about what you should do with it.

route

The regular expression that matched the URI fragment.

originalRoute

If the route was registered as a string, and not a regular expression, then this will be that original string. Otherwise, it is undefined.

params

An object which has keys that are the named parameters from the Route, and corresponding values from the URL.

query

An object representation of the query string in the URI fragment.

queryString

The original query string in the URI fragment. undefined if no query string given

router

The router instance that this route was registered on.

uriFragment

The URI fragment that was matched.