Closed code-beans closed 6 years ago
Hi @code-beans
It seems like you are trying to use to
function in an incorrect way here.
The idea of having a function instead of defining an explicit target state name is useful in some cases, but by design it should not mutate any data.
In your specific case you should use lifecycle events to run the foo
function.
Keep in mind that the to
function should only define a target state is. It should not have any side effects, which foo()
call is.
So the transition should look like this:
{ name: 'next', from: 'mystate', to: 'nextState' }
And lifecycle event handler:
onEnterNextState: function () {
foo();
}
If you have to dynamically define a target state, you can use a function:
{
name: 'next',
from: 'mystate',
to: function () {
if (condition) {
return 'nextState';
} else {
return 'anotherState';
}
}
}
@Leestex I see. Thanks. Maybe this should go into the docs somewhere.
given that a state has a transition where
to
is a function pointer the function will be executed, e.g.Now when
fsm.can('next')
is called,foo()
will be executed which in my understanding it shouldn't. This looks related to #133 as well.