查看父子组合的调度方法



我试图理解寓言应该如何与亲子作文一起工作。当涉及到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) 

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

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

DeploymentView.view deploymentViewModel (DeployContainerView >> dispatch)

这相当于做:

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

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

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

最新更新