-
Notifications
You must be signed in to change notification settings - Fork 966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[question] How is it possible add different data for each state? #149
Comments
I don't think there is a library supported method, but I've done:
|
Try using a function -
|
2024, but another approach is: // Define your data as an array of objects with a name field that corresponds to its state name
const waterCycle = [{name: "water", color: "blue", temperature: 30},
{name: "ice", color: "white", temperature: -10 }]
const machine = new StateMachine({
init: initState,
transitions: transitions,
// Assign the data on the machine to be that array
data: { waterCycle },
// Add a method to access the data based on the current state
methods: {
activeCycleData: function () {
return this.data.find((c) => c.name == this.state);
}
}
}) Then to get the color of the active state you would call: machine.activeCycleData().color Or its temperature: machine.activeCycleData().temperture RelatedYou can call transitions based on it's name by using the syntax: machine["transition name"]() In addition to the machine.transitionName() form from the docs. This is helpful in the case where the transition names are only available at runtime. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
The text was updated successfully, but these errors were encountered: