StateMachine changed - possible to add data?
Hi there,
I'm using Joel Hooks' "robotlegs-utilities-StateMachine", and
was wondering if it's possible to populate the data property of the
StateEvent that is dispatched when the <state
changed="myEvent"> is fired. A non-working example is to
be able to do something similar to the following:
<state name={ HOME } changed={ NavEvent.GO, NavIDEnum.HOME }>
which would make the resulting StateEvent contain {
type:NavEvent.GO, data:NavIDEnum.HOME }
This would allow me to create one command mapping:
commandMap.mapEvent( NavEvent.GO, NavCommand, StateEvent );
and my NavCommand could check the StateEvent.data
property and figure out that in this case we need to navigate to
NavIDEnum.HOME.
Thanks,
Phil
Comments are currently closed for this discussion. You can start a new one.
Support Staff 2 Posted by Joel Hooks on 12 Mar, 2011 03:21 PM
Did you try it? My gut tells me that it won't work out of the box as you expect above.
3 Posted by neil on 12 Mar, 2011 04:11 PM
the changed attribute will just be stringified, and dispatched as the event type.
It shouldn't be to hard to extend / re-write to do as you wish, though.
4 Posted by Philip Bulley on 12 Mar, 2011 05:14 PM
Thanks guys! So I peeked into the code, and that makes sense.
I figured that instead of passing a second parameter through as StateEvent.data, it may be a better idea to use StateEvent.action, as that is always set to null. What are your thoughts on that?
I've made a very small modification so I can setup my state as:
this assigns NavEvent.GO to StateEvent.type and NavIDEnum.HOME to StateEvent.action.
I changed (in StateMachine.as):
to
If a second changed parameter is not specified, or changed is not defined as an array, StateEvent.action is set to undefined (instead of previously always being null), so should be backwards compatible.
I guess the same could easily be done for exiting and entering.
What are your thoughts?
5 Posted by neil on 12 Mar, 2011 05:32 PM
err, does it work?
sorry, my head has been in a very different code base
6 Posted by neil on 12 Mar, 2011 05:48 PM
Actually, best not use Event.action, as it is used for telling the FSM to transition. I would add another property - info, or something like that. That would allow you to still pass proper payloads in the data property
7 Posted by Philip Bulley on 12 Mar, 2011 05:52 PM
Yep it works :) It solves my scenario.
Seeing as it's such a small change, but adds a nice bit of functionality, do you think it could be something useful for the main codebase?
The very slightly modified StateMachine.as is here (http://pastie.org/1663935) if you fancy trying it.
8 Posted by Philip Bulley on 12 Mar, 2011 05:55 PM
I was pondering that regarding StateEvent.action, but thought that as it's never used during StateEvent.CHANGED, I could use it to define a custom action. Or is that confusing?
9 Posted by neil on 12 Mar, 2011 06:12 PM
I think that using the action would be confusing as its name is specific, and your use is against its purpose.
I would get ( if you haven't already ) a github account and fork joel's repo https://github.com/joelhooks/robotlegs-utilities-StateMachine.
then it will be in the public domain and easy to find
10 Posted by Philip Bulley on 12 Mar, 2011 07:06 PM
Will do. Gotta pull myself away form this now, so I'll put it up there tomorrow. For reference it'll be here: https://github.com/milkisevil/
BTW, I'll go with StateEvent.info :)
Thanks for your help guys!
11 Posted by neil on 12 Mar, 2011 10:16 PM
no probs :)
Stray closed this discussion on 02 Apr, 2011 12:00 PM.