4

Grails WebFlow noob here...

One state in my WebFlow receives two events that need to trigger the same action and then transition to separate states if that action is successful. My initial attempt repeated the code in the actionState. Not good. So, after some trial and error, I came up with the following.

    state0 {
        on("event1") {
             flash.stateAfterNext = "state1"
        }.to "actionState"

        on("event2") {
            flash.stateAfterNext = "state2"
        }.to "actionState"
    }

    actionState {
        action {
            flow.unit = Unit.get(params.unit)
            success()
        }
        on("success").to { flash.stateAfterNext }
        on(Exception).to "home"
    }

    state1 { ... }

    state2 { ... }

This works but is it good Grails practice? Is there a better way to handle flow branching logic like this? In particular, should I have used a subflow here, and, if so, what would that look like?

Note: I attempted to move the code in actionState into a separate method but since it references flow that didn't work.

skaffman
  • 398,947
  • 96
  • 818
  • 769
lambmj
  • 1,843
  • 2
  • 21
  • 27

1 Answers1

2

What about something like

flow{    
    state0 {
        on("event1") {
            saveUnit(flow,params.unit)
        }.to "state1"    
        on("event2") {
            saveUnit(flow,params.unit)
        }.to "state2"
    }
    state1{...}
    state2{...}

}

private saveUnit(flow, unit){
    flow.unit = Unit.get(unit)
}
leebutts
  • 4,882
  • 1
  • 23
  • 25