开发者

Grails WebFlow DRY Branching Logic

开发者 https://www.devze.com 2023-02-05 19:50 出处:网络
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 rep

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 开发者_如何学Python"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.


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)
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号