StateRouter

StateRouter

Build Status Join the chat at https://gitter.im/henrytseng/angular-state-router

An AngularJS state-based router designed for flexibility and ease of use.

StateRouter is designed to be used in a modular integration with components:

  • StateView
    • Provides nested view management with template support
  • StateLoadable
    • A lightweight and flexible AngularJS lazy loading scheme.

While not required, StateRouter was originally developed with Browserify.

Installation

To install in your project, simply install from npm

Example

See an example here.

Quick Start

Include the state-router.min.js script tag in your .html:

In app.js add angular-state-router as a dependency when your application module is instantiated.

And define your states and optionally an default initial location

States

StateRouter is design for building applications that can be represented with a finite-state machine model (FSM), a computational model.

States are represented through data objects with an associated dot-notation name. Child states inherit from parent states by default.

Definition

States must be first defined. This is usually done in the angular configuration phase with $stateProvider but can also be done later with $state.

Once a state is defined a transition to the state can be made.

Initialization

Initialization occurs automatically when the application is kicked-started.

$location.url() will be checked for an initial location during initialization, if $location.url() has not been set then an alternative default initial location is used if it exists.

The initialization process is as follows:

  1. Configuration Phase
    • Define states
    • Optionally define "initial location"
  2. Run Phase
    • Initialization, during application kick-started
    • Initial state set as $location.url(), if not empty
    • Initial state falls back to "initial location"

An initialization $stateInit event is broadcasted on $rootScope.

To listen to the init event:

Usage

After states are defined a transition can be made

State changes are asynchronous operations.

Current states can be checked using the active method which accepts a state notation query

And in the same method a state can be triggered using the sref attribute.

Parameters can be sent similarly

Inheritance

States inherit from each other through a parent-child relationship by default; where campus is the parent of campus.classrooms state.

A child state will inherit from it's each of its parents until a inherit value of false value is encountered.

For example, given this definition

We see that campus.classrooms will have a params value

Where availability is inherited from campus, its parent

Resolve

States that include a resolve property will resolve all promises and store results in the locals Object, where they can be accessed $state.current().locals.

locals is has with the following value at the completion of the state transition:

Actions

States that include an action property will execute additional actions before a .

States may often require additional actions to be performed during transition. These actions typically do not expose data like resolve property but may execute other services.

Events

Events are broadcast on the $rootScope.

$stateInit

This event is emitted when $state is initialized. If an initial state is specified $stateInit occurs after the current state is set.

$stateChangeBegin

  • request Object Requested data { name: 'nextState', params: {} }

This event is emitted when a requested change to a valid state exists.

$stateChangeError

  • request Object Requested data { name: 'nextState', params: {} }

This event is emitted whenever an error occurs.

$stateChangeErrorNotFound

  • request Object Requested data { name: 'nextState', params: {} }

This event is emitted when a state cannot be found and no parent state(s) exist.

$stateChangeErrorResolve

This event is emitted when an error occurred during resolve.

$stateChangeEnd

  • request Object Requested data { name: 'nextState', params: {} }

This event occurs when a valid state change successfully finishes. This event does not trigger when an error was encountered. Use the 'change' event for all change requests.

$stateReload

This event is broadcasted when the current state is reloaded.

$stateChangeComplete

  • error Object Null if successful, Error object if error occurs
  • request Object Requested data { name: 'nextState', params: {} }

This event occurs when a state change is finished. This event is always triggered on any change request. Also occurs after 'error' is emitted.

API Services

$stateProvider

Configuration phase setup of StateRouter.

$stateProvider#options(options)

  • @param {Object} options A data Object
  • @return {$stateProvider} Itself; chainable

Set configuration data parameters for StateRouter

Available options include

  • historyLength {Number} Defaults to 5
  • initialLocation {Object} An Object{name:String, params:Object} for initial state transition

Chainable.

$stateProvider#state

  • @param {String} name A unique identifier for the state; using state-notation
  • @param {Object} data A state definition data Object
  • @return {$stateProvider} Itself; chainable

Define or get a state. Chainable.

$stateProvider#init

  • @param {String} name A iniital state
  • @param {Object} params A data object of params
  • @return {$stateProvider} Itself; chainable

Set initialization parameters; deferred to $ready()

$state

Run phase access to StateRouter.

$state#options

  • @return {Object} A configured options

Get options

$state#state

  • @param {String} name A unique identifier for the state; using state-notation
  • @param {Object} data A state definition data Object
  • @return {$state} Itself; chainable

Set/get state. Reloads state if current state is affected by defined state (when redefining parent or current state)

$state#$use

  • @param {Function} handler A callback, function(request, next)
  • @param {Number} priority A number denoting priority
  • @return {$state} Itself; chainable

Internal method to add middleware; called during state transition

$state#change

  • @param {String} name A unique identifier for the state; using dot-notation
  • @param {Object} [params] A parameters data object
  • @return {Promise} A promise fulfilled when state change complete

Request state transition, asynchronous operation

$state#reload

  • @return {Promise} A promise fulfilled when state change occurs

Reload the current state.

$state#$location

  • @param {String} url A url matching defind states
  • @param {Function} [callback] A callback, function(err)
  • @return {$state} Itself; chainable

Internal method to change state based on $location.url(), asynchronous operation using internal methods, quiet fallback.

$state#current

  • @return {Object} A copy of current state

Retrieve copy of current state

$state#active

  • @param {Mixed} query A string using state notation or a RegExp
  • @param {Object} params A parameters data object
  • @return {Boolean} A true if state is parent to current state

Check query against current state

API Directives

sref

  • sref {String} A defined state to transition to, using state name notation

Request a state transition when clicked.

Example

A state transition to products.items with parameters {catalog:'1-aeff', item:'e32537'}

State Notation

States use dot-notation where state names are /[a-zA-Z0-9_]*/ strings separated by dots . and are case sensitive.

The following are examples of valid unique state names:

Parameters

Data Objects can be included in an expression (not query) given by Object literal notation. Using name-value pairs of Boolean/Number/String/null.

They are expressed using parenthesis () surrounding the Object literal at the end of a state expression.

Queries

The following are examples of state notation queries that would match the state ochestra1.trombone.position.6

Wildcards

Queries can also use wildcards * to match any one state or ** to match any pattern of states following or preceding.

Both of the following will match the state catalog.index.list

URLs

URLs in state definitions take the form:

Where parameters are specified in URLs using variable names starting with a colon, e.g. - :id. And a default value can be specified using a params Object.

To retrieve the current state and its parameter values use (e.g. - for example finding the value of eventid):

Query String

Query string values are also set in the params object.

Given the URL http://test.com/#/events/birthday_event?color=blue

Components

Components register themselves as middleware layers and respond to state changes.

Building a Custom Component

To build your own components simply register your the middleware with the $state.$use() method.

$use expects a function signature function(request, next) where request is data Object containing data for the current state transition and next is a completion callback.

Component operate asynchronously and next must be called.

Browserify

To use Browserify require the module directly. For example:

Contribute

If you've got ideas on how to make StateRouter better create an issue and mark an enhancement in Github.

If there are any unreported errors please let us know. We'd like StateRouter to give as much feedback as possible to eliminate common problems that may occur during development.

To get start programming, build

To get started watch files for programming

To host the example

Then using your browser visit http://localhost:3000/index.html

License

Copyright (c) 2015 Henry Tseng

Released under the MIT license. See LICENSE for details.

comments powered by Disqus
This page was last updated almost 2 years ago.