MObjectStates

MObjectStates ⇐ Trait

This class represents an helper for any object that needs to have states.

Important:

  • the class MUST DEFINE a static variable named STATES (which must be an array of allowed states)
  • the class must define all states individually with correct index, ie ie STATES:[“STATE_READY”,“STATE_ERROR”] => STATE_READY:0 and STATE_ERROR:1 constants
  • Make sure to concatenate states in case of inheritance (see below)

Kind: global mixin
Extends: Trait
Properties

NameTypeDescription
_statenumber

current state

Example

Class.declare("AbstractClassWithStates", {
     traits: [$MObjectStates],

     statics: {
         STATE_READY: 0,
         STATES: ["STATE_READY"]
     },
     constructor: function(properties) {
         this.setStateByName("STATE_READY");
     }
});

$AbstractClassWithStates.declare("MyClassWithStates", {
     statics: {
         STATE_READY: $AbstractClassWithStates.STATE_READY,
         STATE_ERROR:1,
         STATES: $AbstractClassWithStates.STATES.concat(["STATE_ERROR"])
     }
});

var instance = $MyClassWithStates();
instance.getState();                                     // $MyClassWithStates.STATE_READY
instance.getStateByName();                               // STATE_READY
instance.setState($MyClassWithStates.STATE_ERROR);
instance.setStateByName("STATE_ERROR");