包详细信息

abstract-state-router

TehShrike1.5kWTFPL8.0.4

Like ui-router, but without all the Angular. The best way to structure a single-page webapp.

router, ui-router

自述文件

ChangelogJoin the chat on DiscordAPI documentation

Brief explanation

abstract-state-router lets you build single-page webapps using nested routes/states. Your code doesn't reference routes directly, like /app/users/josh, but by name and properties, like app.user + { name: 'josh' }.

Nested routes were a novelty when abstract-state-router released in 2014! If you're interested in the motivation, check out Why Your Webapp Needs a State-Based Router.

abstract-state-router has another huge advantage: you can use it with any templating/component library you like, and transition your app from one component library to another gradually when the day comes.

Project status

This project is stable and has been used in production for years.

The last major version bump was in April of 2025 when the project was finally converted from CommonJS to ES Modules and support for callbacks was dropped (nearly 10 years after Promises were added to the language spec).

abstract-state-router is extensible without much work, so very few feature additions have been necessary.

Current renderer implementations

If you want to use the state router with any other templating/dom manipulation library, read these docs! It's not too bad to get started.

Install

npm i abstract-state-router

Your CommonJS-supporting bundler should be able to import make_state_router from 'abstract-state-router' without issue.

API

Instantiate

import createStateRouter from 'abstract-state-router'

const stateRouter = createStateRouter(makeRenderer, rootElement, options)

The makeRenderer should be a function that returns an object with these properties: render, destroy, and getChildElement. Documentation is here - see test/support/renderer-mock.js for an example implementation.

The rootElement is the element where the first-generation states will be created.

options

Possible properties of the options object are:

  • pathPrefix defaults to '#'. If you're using HTML5 routing/pushState, you'll most likely want to set this to an empty string.
  • router defaults to an instance of a hash brown router@3.x. The abstract-state-router unit tests use the hash brown router stub. To use pushState, pass in a hash brown router created with sausage-router.
  • throwOnError defaults to true, because you get way better stack traces in Chrome when you throw than if you console.log(err) or emit 'error' events. The unit tests disable this.

stateRouter.addState({name, route, defaultChild, data, template, resolve, activate, querystringParameters, defaultParameters, canLeaveState})

The addState function takes a single object of options. All of them are optional, unless stated otherwise.

name is parsed in the same way as ui-router's dot notation, so 'contacts.list' is a child state of 'contacts'. Required.

route is an express-style url string that is parsed with a fork of path-to-regexp. If the state is a child state, this route string will be concatenated to the route string of its parent (e.g. if 'contacts' state has route ':user/contacts' and 'contacts.list' has a route of '/list', you could visit the child state by browsing to '/tehshrike/contacts/list').

defaultChild is a string (or a function that returns a string) of the default child's name. Use the short name (list), not the fully qualified name with all its parents (contacts.list).

If the viewer navigates to a state that has a default child, the router will redirect to the default child. (For example, if 'list' is the default child of 'contacts', state.go('contacts') will actually be equivalent to state.go('contacts.list'). Likewise, browsing to '/tehshrike/contacts' would take the viewer to '/tehshrike/contacts/list'.)

data is an object that can hold whatever you want - it will be passed in to the resolve and activate functions.

template is a template string/object/whatever to be interpreted by the render function. Required.

resolve is a function called when the selected state begins to be transitioned to, allowing you to accomplish the same objective as you would with ui-router's resolve.

activate is a function called when the state is made active - the equivalent of the AngularJS controller to the ui-router.

querystringParameters is an array of query string parameters that will be watched by this state.

defaultParameters is an object whose properties should correspond to parameters defined in the querystringParameters option or the route parameters. Whatever values you supply here will be used as the defaults in case the url does not contain any value for that parameter. If you pass a function for a default parameter, the return of that function will be used as the default value.

canLeaveState is an optional function that takes two arguments: the state's domApi, and an object with the name and parameters of the state that the user is attempting to navigate to. It can return either a boolean, or a promise that resolves to a boolean. If canLeaveState returns false, navigation from the current state will be prevented. If the function returns true the state change will continue.

resolve(data, parameters)

data is the data object you passed to the addState call. parameters is an object containing the parameters that were parsed out of the route and the query string.

Returning values

Your resolve function must return a promise. Properties on the resolved object will be set as attributes on the state's component.

async function resolve(data, parameters) {
    const [ user, invoice ] = await Promise.all([
        fetchUser(parameters.userId),
        fetchInvoice(parameters.invoiceId),
    ])

    return {
        user,
        invoice,
    }
}

Errors/redirecting

If you return a rejected promise the state change will be cancelled and the previous state will remain active.

If you return a rejected promise with an object containing a redirectTo property with name and params values, the state router will begin transitioning to that state instead. The current destination will never become active, and will not show up in the browser history:

async function resolve(data, parameters) {
    throw {
        redirectTo: {
            name: 'otherCoolState',
            params: {
                extraCool: true
            }
        }
    }
}

activate(context)

The activate function is called when the state becomes active. It is passed an event emitter named context with four properties:

  • domApi: the DOM API returned by the renderer
  • data: the data object given to the addState call
  • parameters: the route/querystring parameters
  • content: the object returned by the resolve function

The context object is also an event emitter that emits a 'destroy' event when the state is being transitioned away from. You should listen to this event to clean up any workers that may be ongoing.

addState examples

stateRouter.addState({
    name: 'app',
    data: {},
    route: '/app',
    template: '',
    defaultChild: 'tab1',
    async resolve(data, parameters) {
        return isLoggedIn()
    },
    activate(context) {
        // Normally, you would set data in your favorite view library
        var isLoggedIn = context.content
        var ele = document.getElementById('status')
        ele.innerText = isLoggedIn ? 'Logged In!' : 'Logged Out!'
    }
})

stateRouter.addState({
    name: 'app.tab1',
    data: {},
    route: '/tab_1',
    template: '',
    async resolve(data, parameters) {
        return getTab1Data()
    },
    activate(context) {
        document.getElementById('tab').innerText = context.content

        var intervalId = setInterval(function() {
            document.getElementById('tab').innerText = 'MORE CONTENT!'
        }, 1000)

        context.on('destroy', function() {
            clearInterval(intervalId)
        })
    }
})

stateRouter.addState({
    name: 'app.tab2',
    data: {},
    route: '/tab_2',
    template: '',
    async resolve(data, parameters) {
        return getTab2Data()
    },
    activate(context) {
        document.getElementById('tab').innerText = context.content
    }
})

stateRouter.go(stateName, [stateParameters, [options]])

Browses to the given state, with the current parameters. Changes the url to match.

The options object currently supports two options:

  • replace - if it is truthy, the current state is replaced in the url history.
  • inherit - if true, querystring parameters are inherited from the current state. Defaults to false.

If a state change is triggered during a state transition, and the DOM hasn't been manipulated yet, then the current state change is discarded, and the new one replaces it. Otherwise, it is queued and applied once the current state change is done.

If stateName is null, the current state is used as the destination.

stateRouter.go('app')
// This actually redirects to app.tab1, because the app state has the default child: 'tab1'

stateRouter.evaluateCurrentRoute(fallbackStateName, [fallbackStateParameters])

You'll want to call this once you've added all your initial states. It causes the current path to be evaluated, and will activate the current state. If no route is set, abstract-state-router will change the url to to the fallback state.

stateRouter.evaluateCurrentRoute('app.home')

stateRouter.stateIsActive([stateName, [stateParameters]])

Returns true if stateName is the current active state, or an ancestor of the current active state...

...And all of the properties of stateParameters match the current state parameter values.

You can pass in null as the state name to see if the current state is active with a given set of parameters.

// Current state name: app.tab1
// Current parameters: { fancy: 'yes', thing: 'hello' }
stateRouter.stateIsActive('app.tab1', { fancy: 'yes' }) // => true
stateRouter.stateIsActive('app.tab1', { fancy: 'no' }) // => false
stateRouter.stateIsActive('app') // => true
stateRouter.stateIsActive(null, { fancy: 'yes' }) // => true

stateRouter.makePath(stateName, [stateParameters, [options]])

Returns a path to the state, starting with an optional octothorpe #, suitable for inserting straight into the href attribute of a link.

The options object supports one property: inherit - if true, querystring parameters are inherited from the current state. Defaults to false.

If stateName is null, the current state is used.

stateRouter.makePath('app.tab2', { pants: 'no' })

stateRouter.getActiveState()

Returns the last completely loaded state

// Current state name: app.tab1
// Current state params: pants: 'no'
stateRouter.getActiveState() // => { name: 'app.tab1', parameters: { pants: 'no' }}

Events

These are all emitted on the state router object.

State change

  • stateChangeAttempt(functionThatBeginsTheStateChange) - used by the state transition manager, probably not useful to anyone else at the moment
  • stateChangeStart(state, parameters, states) - emitted after the state name and parameters have been validated
  • stateChangeCancelled(err) - emitted if a redirect is issued in a resolve function
  • stateChangeEnd(state, parameters, states) - after all activate functions are called
  • stateChangePrevented(oldState: { name, parameters }, attemptedNavigationState: { name, parameters })
  • stateChangeError(err) - emitted if an error occurs while trying to navigate to a new state - including if you try to navigate to a state that doesn't exist
  • stateError(err) - emitted if an error occurs in an activation function, or somewhere else that doesn't directly interfere with changing states. Should probably be combined with stateChangeError at some point since they're not that different?
  • routeNotFound(route, parameters) - emitted if the user or some errant code changes the location hash to a route that does not have any states associated with it. If you have a generic "not found" page you want to redirect people to, you can do so like this:
stateRouter.on('routeNotFound', function(route, parameters) {
    stateRouter.go('not-found', {
        route: route,
        parameters: parameters
    })
})

DOM API interactions

  • beforeCreateState({state, content, parameters})
  • afterCreateState({state, domApi, content, parameters})
  • beforeDestroyState({state, domApi})
  • afterDestroyState({state})

Testing/development

To run the unit tests:

  • clone this repository
  • run npm install
  • run npm test

State change flow

  • emit stateChangeStart
  • call all resolve functions
  • resolve functions return
  • NO LONGER AT PREVIOUS STATE
  • destroy the contexts of all "destroy" states
  • destroy appropriate dom elements
  • call render functions for "create"ed states
  • call all activate functions
  • emit stateChangeEnd

Every state change does this to states

  • destroy: states that are no longer active at all. The contexts are destroyed, and the DOM elements are destroyed.
  • create: states that weren't active at all before. The DOM elements are rendered, and resolve/activate are called.

HTML5/pushState routing

pushState routing is technically supported. To use it, pass in an options object with a router hash-brown-router constructed with a sausage-router, and then set the pathPrefix option to an empty string.

import makeStateRouter from 'abstract-state-router'
import sausage from 'sausage-router'
import makeRouter from 'hash-brown-router'

const stateRouter = makeStateRouter(makeRenderer, rootElement, {
    pathPrefix: '',
    router: makeRouter(sausage())
})

However to use it in the real world, there are two things you probably want to do:

Intercept link clicks

To get all the benefits of navigating around nested states, you'll need to intercept every click on a link and block the link navigation, calling go(path) on the sausage-router instead.

You would need to add these click handlers whenever a state change happened.

Server-side rendering

You would also need to be able to render the correct HTML on the server-side.

For this to even be possible, your chosen rendering library needs to be able to work on the server-side to generate static HTML. I know at least Ractive.js and Riot support this.

The abstract-state-router would need to be changed to supply the list of nested DOM API objects for your chosen renderer.

Then to generate the static HTML for the current route, you would create an abstract-state-router, tell it to navigate to that route, collect all the nested DOM API objects, render them as HTML strings, embedding the children inside of the parents.

You would probably also want to send the client the data that was returned by the resolve functions, so that when the JavaScript app code started running the abstract-state-router on the client-side, it wouldn't hit the server to fetch all the data that had already been fetched on the server to generate the original HTML.

Who's adding this?

Track development progress in #48.

It could be added by me, but probably not in the near future, since I will mostly be using this for form-heavy business apps where generating static HTML isn't any benefit.

If I use the abstract-state-router on an app where I want to support clients without JS, then I'll start working through those tasks in the issue above.

If anyone else has need of this functionality and wants to get keep making progress on it, I'd be happy to help. Stop by the chat room to ask any questions.

Maintainers

License

WTFPL

更新日志

8.0.4

  • Export a Redirect type that you can throw from your resolve functions

8.0.3

  • Update the types to reflect the fact that the resolve function doesn't get passed a callback function with a redirect property as of 8.0.0

8.0.2

  • fix a bug where default parameters from parent states were being ignored #169
    • if you navigated to state1.child2.grandchild3, the only default parameters being used were the ones on grandchild3
  • remove support for defaultQuerystringParameters in state definitions #168
    • technically this should be a breaking change, and I wish we'd remembered to do it during the 8.0.0 release, but I don't feel too bad about doing it now because
      • it was deprecated 8 years ago
      • the type definition doesn't include it, so anyone using TS is already guaranteed to not be using it
      • anyone continuing to use it will get a runtime warning in their console about it

8.0.1

  • Fix: pass the correct state name into getDomChild #167

8.0.0

  • drop callbacks, everything is promises now #162
  • drop CJS, use ESM everywhere #166
  • added TypeScript types

7.7.1

  • Fixed lying changelog

7.7.0

  • Accidentally published this new version because I wasn't paying attention to branch names when I merged a PR. No real changes occurred

7.6.0

  • feature: the canLeaveState function now gets passed a second argument with the name+parameters of the state that the user is trying to navigate to
  • technically a breaking change: the previously undocumented stateChangePrevented function now gets passed a name+parameters object as its first argument, instead of just the name of the current state. Also, it gets passed a second argument with the name+parameters of the state that the user attempted to visit.

7.5.2

Fix: only call canLeaveState on states that are going to be destroyed, not the ones that are going to get created.

7.5.1

Fix a bug that could cause canLeaveState to be called twice. #155

7.5.0

Add canLeaveState to addState, so you can implement route guards to prevent people from navigating away from a state if they e.g. have unsaved state. #154

7.4.0

Support passing in a null state name to stateIsActive.

7.3.1

Fixes an issue where when a state was destroyed and resolved during the same state change, the result of its resolve function would get tossed out.

7.3.0

Removes the concept of resetting states. #152

The concept of resetting breaks down if your component library doesn't support

  • slots
  • resetting the state of a component without resetting the contents of slots

For renderers that "reset" states by destroying the existing component and re-constructing it, stuff would break in any case where a parent and child state were both told to reset at once. Whenever the parent would reset and destroy its part of the DOM, the child would get wiped out.

Existing renderers don't need to change to work with this version of ASR, it's just that their reset function won't get called any more.

The beforeResetState and afterResetState events should not be fired any more.

7.2.0

  • Coerce parameter values to strings for comparison in stateIsActive #151

7.1.0

  • Give an explicit/useful error message if you forget to add a ui-view element to a parent template #148

7.0.0

  • maintenance: update a bunch of dependencies #141
  • maintenance: autoformat the scripts to make myself feel better and reduce commit noise in the future #145
  • breaking: dropped ES5 support. If you're targeting ES5 you'll need to compile it in your own app's build.

6.2.0

  • feature: allow dynamic parameter defaults via functions #144

6.1.0

  • feature: added getActiveState method #121

6.0.5

  • maintenance: added a bunch of files to the .npmignore to reduce the package download size

6.0.4

  • bug fix: update the hash-brown-router dependency to fix an issue where calling evaluateCurrent('somestate') wouldn't do anything when somestate's route was / and the current url was also /. Issue #116 in abstract-state-router, commit #56c207f0 in hash-brown-router. If you're passing in your own hash-brown-router instance, make sure to update it to 3.3.1 to avoid the bug.

6.0.3

  • bug fix: point package.main at the ES5 bundle instead of the ES2015 code :-x 34ea0baa

6.0.2

  • bug fix: fixed a crash that would happen if you didn't pass an options object in 3b60669b

6.0.1

  • dependency update: changed hash-brown-router dependency from ~3.2.0 to ^3.3.0 a593408b

6.0.0

  • Promise and Object.assign polyfills are now required for older browsers
  • refactor: updated all the source code to ES2015 (though the published version is still compiled down to ES5)

5.17.0

  • functional: stateChangeStart and stateChangeEnd events now also emit the full array of states being transitioned to. #113

5.16.3

  • documentation: added a table of contents to the API section of the readme #111

5.16.2

  • drop dependencies on the process and events polyfills and bump hash-brown-router dependency, saving about 25KB

5.16.1

  • documentation: fixed a requre/require typo #103
  • documentation: added "inherit" to the documented go() options #104
  • documentation: in the rendered docs, fixed the link to the ractive example source code 8511b651

5.16.0

  • updated hash-brown-router to 3.1.0, making the / route equivalent to an empty route string #102

5.15.1

  • updated hash-brown-router #93
  • compatibility: switched from require('events').EventEmitter to require('events') for better Rollup compatibility. c861f5ab

5.15.0

  • functional: renderers may now return a new DOM API from the reset function. c07a45fb

5.14.1

  • bug fix: empty strings in default parameters would cause the state router to stop cold without any error message #2abf9361

5.14.0

  • functional: replaced the defaultQuerystringParameters property on states with defaultParameters, which applies to both querystring and route parameters. If you don't specify defaultParameters, defaultQuerystringParameters will be checked too (though it will now apply to route parameters as well). #91

5.13.0

  • functional: made stateName optional for go/replace/makePath #83

5.12.5

  • dependency update: require page-path-builder 1.0.3, fixing a path-building bug #650af6af

5.12.4

  • bug fix: stateIsActive was doing an extremely naive check #71

5.12.3

  • bug fix: makePath(stateName, params, { inherit: true }) now properly inherits route parameters during the render/activate phases #7617d74b

5.12.2

  • bug fix: fixed Webpack build by changing a JSON file to CommonJS #65

5.12.1

  • bug fix: states that had child states without routes weren't necessarily loading the correct child state when you browsed to them #85112c79

5.12.0

5.11.0

5.10.0

5.9.0

5.8.1

5.8.0

  • functional: changed parameters objects passed to the DOM rendering functions to be mutable copies instead of being frozen