parent child 组合的调度方法

dispatch method in view for parent child composition

我想了解寓言应该如何与 parent child 组合一起使用。当涉及到 update 方法、init 和命令定义时,事情就很简单了。但是 view 方法及其 dispatch 方法很难找出

在我的代码中,child 是:

module DeploymentView

type DeploymentTypeView =
    | DeployContainerView

type Model = { 
 CurrentView : DeploymentTypeView option
}

type Msg =
| ShowDeployContainer

let init () : Model =
    let initialModel = { 
        CurrentView = None
    }
    initialModel


let update (msg : Msg) (currentModel : Model) : Model * Cmd<Msg> = 
    match msg with 
    | ShowDeployContainer ->
        let nextModel = { 
            currentModel with CurrentView = Some DeployContainerView 
        }
        nextModel, Cmd.none     
    | _ -> currentModel, Cmd.none


let view (model : Model) (dispatch : Msg -> unit) =
    [
        Content.content [ Content.Modifiers [ Modifier.TextAlignment (Screen.All, TextAlignment.Left) ] ]
            [ 
                Heading.h3 [] [ str ("Deployments: ") ] 
            ] 

        Columns.columns []
            [ 
                Column.column [] [ button "deploy container" (fun _ -> dispatch ShowDeployContainer) ]
            ] 
    ]

然后 documentation 关于 parent child 处理我定义了一个 parent 像这样:

module Client

type PortalView =
| DeploymentView of DeploymentView.Model
| ProductAdministrationView


type Model = { 
    CurrentPortal : PortalView option
}

// The Msg type defines what events/actions can occur while the application is running
// the state of the application changes *only* in reaction to these events
type Msg =
| ShowDeployment
| ShowAdministration
| DeployContainerView of DeploymentView.Msg


// defines the initial state and initial command (= side-effect) of the application
let init () : Model * Cmd<Msg> =
    let initialModel = { 
        CurrentPortal = None 
    }
    initialModel, Cmd.none

let update (msg : Msg) (currentModel : Model) : Model * Cmd<Msg> =
    match  msg with
    | ShowDeployment ->
        let nextModel = { 
            currentModel with CurrentPortal = Some <| DeploymentView(DeploymentView.init())
        }
        nextModel, Cmd.none
    | ShowAdministration ->
        let nextModel = { 
            currentModel with CurrentPortal = Some ProductAdministrationView
        }
        nextModel, Cmd.none
    | DeployContainerView msg' ->
        let res, cmd = 
            match currentModel.CurrentPortal with
            | Some(DeploymentView(m)) -> DeploymentView.update msg' m
            | _ -> DeploymentView.init(), Cmd.none
        { currentModel with CurrentPortal = Some(DeploymentView(res)) }, Cmd.map DeployContainerView cmd

到目前为止一切顺利,我的问题出现在渲染视图本身时。 客户端视图使用函数如下:

let view (model : Model) (dispatch : Msg -> unit) 

其中 MsgDeploymentView.Msg 类型,而在 parent 视图中我可以访问 Client.Msg -> unit 类型的调度。如何分解 parent 调度以将其映射到 child 调度签名?

您可以使用 >> 运算符非常轻松地创建符合 child 期望的调度函数:

DeploymentView.view deploymentViewModel (DeployContainerView >> dispatch)

相当于做:

DeploymentView.view deploymentViewModel (fun msg -> msg |> DeployContainerView |> dispatch)

也就是说,它将 child 的消息包装在 DeployContainerView 中,然后将其传递给 dispatch

另一方面,在用于包装 child msg 类型的构造函数上使用 Msg 后缀是一种常见且良好的约定。您可能需要考虑将 DeployContainerView 重命名为 DeploymentContainerMsg.