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.statemanager

v0.0.7

Published

Simple, powerful state management for Backbone.js

Downloads

12

Readme

Backbone.StateManager

Simple, powerful state management for Backbone.js

About StateManager

Backbone.StateManager is a module for Backbone.js that adds the ability to easily manage and utilize states in any size JavaScript application. It can be used as a stand alone object or in conjunction with a target object through its addStateManager method.

Key Benefits

  • Modular definitions of states
  • Sub/pub architecture with Backbone.Events
  • Support for transition events between states
  • RegExp matching for states and transitions
  • Easy to attach to any object

Compatibility and Requirements

Backbone.StateManager currently has the following dependencies:

Source Code and Downloads

Backbone.StateManager is written in CoffeeScript. You can download the raw source code from the "src" folder or download the JavaScript build in the main directory.

The latest stable releases can be found at the links:

Getting Started

Backbone.StateManager constructor takes two arguments, a state object and an options object, but neither is required. Passed in states will be automatically added and the options are set as an instance property.

  stateManager = new Backbone.StateManager
  # or
  states =
    foo :
      enter : -> console.log 'enter bar'
      exit : -> console.log 'exit foo'
    bar :
      enter : -> console.log 'enter bar'
      exit : -> console.log 'exit bar'

  stateManager = new Backbone.StateManager states

Defining a State

A state is intended to be as modular as possible, so each state is expected to contain enter and exit methods that are used when entering or leaving that state. A state definition can also have a transitions property that contains several methods to be used when moving between specified states.

  {
    enter : -> console.log 'enter'
    exit : -> console.log 'exit'
    transitions :
      'onBeforeExitTo:anotherState' : -> # method to be called before exit to `anotherState`
      'onExitTo:anotherState' : -> # method to be called on exit to `anotherState`
      'onBeforeEnterFrom:anotherState' : -> # method to be called before entering from `anotherState`
      'onEnterFrom:anotherState' : -> # method to be called on entering from `anotherState`
  }

Defining State Transitions

Transitions are used to execute additional functionality when moving between specified states. There are 4 types of transitions that Backbone.StateManager will defaultly look for: onBeforeExitTo, onExitTo, onBeforeEnterFrom, and onEnterFrom. Each transition is a key value pair, where the value is a method and the key defines the transition type and the specified state (e.g. onEnterFrom:specifiedState).

Adding a State

New states can be added individually using addState and passing the name of the state and a state object as defined above.

  stateManager.addState name, definition

Triggering a State

A state is triggered using triggerState and passing the name of the state and options. If the requested state is already the currentState, no methods will be executed. This can be overriden by passing in the option reEnter : true to the method.

  stateManager.triggerState name, options

Removing a State

A states can be added using removeState and passing in the name of the state.

  stateManager.removeState name

Using with Objects

StateManager provides an easy method to painlessly add a StateManager to any object. StateManager.addStateManager takes a target object and an optional set of options, reads in any states defined on the target, and creates a new StateManager. It also sets a number of methods on target, including triggerState, getCurrentState, and a reference to the StateManager at target.stateManager.


View = Backbone.View.extend
  states :
    foo :
      enter : -> console.log 'enter bar'
      exit : -> console.log 'exit foo'
      transitions :
        'onExitTo:bar' : -> 'just exited and bar is about to be entered'
    bar :
      enter : -> console.log 'enter bar'
      exit : -> console.log 'exit bar'

  initialize : -> Backbone.StateManager.addStateManager @

Note: Similar to Backbone.js' defaults attribute, the states object will be shared among all instances of this state-managed view. Instead, define states as a function that returns an object consisting of your state definitions.

Github Issues

Development

# From the project's dir
npm install && bower install

Build tool

This project uses Gulp.js for it's build tool

To build:

gulp build

To run tests:

gulp test

To build, run tests, and watch for changes:

gulp