1

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

在我的代码中,孩子是:

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) ]
            ] 
    ]

在这个关于父子处理的文档之后,我定义了一个像这样的父:

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) 

where Msgis of typeDeploymentView.Msg 而在父视图中我可以访问 type 的调度Client.Msg -> unit。如何分解父调度以将其映射到子调度签名?

4

1 回答 1

1

通过使用操作符,您可以非常轻松地创建一个符合孩子期望的调度函数>>

DeploymentView.view deploymentViewModel (DeployContainerView >> dispatch)

这相当于做:

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

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

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

于 2018-12-08T00:40:44.137 回答