Giter Site home page Giter Site logo

backbone.statemanager's Introduction

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 it's 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 (eg 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 @

backbone.statemanager's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.