Grails WebFlow DRY Branching Logic

Grails WebFlow noob here ...

One state in my WebFlow receives two events that should trigger the same action, and then go to separate states if this action is successful. My initial attempt repeated the code in 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 { ... } 

It works, but how good is Grails practice? Is there a better way to handle branching logic? In particular, should I use a subflow here, and if so, what will it look like?

Note. I tried moving the code in actionState to a separate method, but since it refers to flow , which does not work.

+4
source share
1 answer

Sort of

 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) } 
+2
source

Source: https://habr.com/ru/post/1336203/


All Articles