Using the State Machine
Trace: » Get Help Using Appcelerator Entourage » Get Started with Zend Framework » Entourage UI JavaScript API » Selectable » Using the State Machine

Using the State Machine

In a rich client application, it is often useful to track the current state of an application, such as “logged in” or “unauthenticated”. Entourage provides a State Machine component to enable this programming technique, and allow you to create stateful client applications in the web browser.

Live Example

Waiting for message...

Source Code

<p>
<input id="set_active" type="button" value="Activate" on="click then l:active"/>
<input id="set_inactive" type="button" value="Inactivate" on="click then l:inactive"/>
<input id="expression" type="button" value="Inactivate (Expression)" 
  on="click then statechange[sm=inactive]"/>
</p>
<div id="messageBox" style="border:1px solid;padding:5px">
  Waiting for message...
</div>
 
<script>
jQuery(function() {
  var sm = new App.StateMachine('sm');
 
  sm.addListener(function() {
    jQuery('#messageBox').text(this.getActiveState());
  });
  sm.addState('active', 'l:active', true);
  sm.addState('inactive', 'l:inactive', true);
});
</script>

App.StateMachine(name)

Create a new State Machine with the given name.

var my_state_machine = App.StateMachine('state_machine');
Parameter Description
name A string which is the name of the state machine.


addState(state,trigger,active)

Add a state to the state machine.

my_state_machine.addState('a_state','l:message[name=value,name2!=value]', false);
Parameter Description
state A string which is the the name of the state.
trigger A string which defines message and any parameters to trigger the state change. Takes the form of l:message[name=value,name2!=value].
active A boolean identifying if the state should become the active state.


addListener(callback)

Adds a listener to the state machine.

my_state_machine.addListener(function(){
  // ...
});
Parameter Description
callback A function that will be called when the state machine state changes.


fireStateChange(state)

Changes the state machine's state to the passed in state.

my_state_machine.fireStateChange('a_state');
Parameter Description
state A string which is the the name of the state to fire.


getActiveState()

Returns the active state.

my_state_machine.getActiveState();


setActiveState(state)

Another way to call fireStateChange(state).

my_state_machine.setActiveState('a_state');
Parameter Description
state A string which is the the name of the state to fire.
expressions/statemachine.txt · Last modified: 2009/03/11 10:55 by kwhinnery