我试图理解寓言应该与亲子组成一起工作的方式。当涉及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) ]
]
]
然后在关于父级子处理的documentation之后,我定义了这样的父级:
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)
其中Msg
的类型为DeploymentView.Msg
,而在父视图中,我可以访问类型为Client.Msg -> unit
的调度程序。如何分解父调度以将其映射到子调度签名?
答案 0 :(得分:1)
通过使用echo "<button type='button' onclick=\"location.href='http://www.example.com'\" class='single_add_to_cart_button button alt wdm_enquiry' style='background:rgb(0, 171, 0) none repeat scroll 0% 0%';>Ihre $post_name Testversion anfordern!</button>";
运算符,您可以非常轻松地创建符合孩子期望的调度功能:
>>
等效于:
DeploymentView.view deploymentViewModel (DeployContainerView >> dispatch)
也就是说,它将孩子的消息包装在DeploymentView.view deploymentViewModel (fun msg -> msg |> DeployContainerView |> dispatch)
中,然后将其传递到DeployContainerView
。
另一方面,在用于包装子dispatch
类型的构造函数上使用Msg
后缀是一种常见且良好的约定。您可能需要考虑将msg
重命名为DeployContainerView
。