jakesgordon / javascript-state-machine

A javascript finite state machine library
MIT License
8.69k stars 964 forks source link

[question] How is it possible add different data for each state? #149

Open menteora opened 6 years ago

menteora commented 6 years ago

I took your guide that speak about single data shared from all states or passed on initialization, but I don't understand how to define one color for each state, for example.

Thanks You

Guide Reference: http://github.com/jakesgordon/javascript-state-machine/blob/master/docs/data-and-methods.md

miniatureape commented 6 years ago

I don't think there is a library supported method, but I've done:

let machine = StateMachine({
   ... state setup ...
    data: {
        firstState: {a: 1}, 
        secondState: {}
    },
    methods: {
        onFirstState: function(lifecycle) {
             // logs {a: 1}
             console.log(this[lifecycle.to]);
        }
    }
})
rickbsgu commented 6 years ago

Try using a function -


data: function() { return {
  firstState: { a: 1},
  secondState: {}
}};