javascript-state-machine icon indicating copy to clipboard operation
javascript-state-machine copied to clipboard

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

Open menteora opened this issue 8 years ago • 3 comments

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

menteora avatar Feb 01 '18 17:02 menteora

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]);
        }
    }
})

miniatureape avatar Feb 25 '18 22:02 miniatureape

Try using a function -

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

rickbsgu avatar Feb 25 '18 23:02 rickbsgu