在elm中链接html事件的联合类型
我不确定这是否可行,但我正在尝试使用链式联合类型触发html事件。我有以下两种接头类型:在elm中链接html事件的联合类型,elm,Elm,我不确定这是否可行,但我正在尝试使用链式联合类型触发html事件。我有以下两种接头类型: type DialogOf = SearchItems | SearchingItems String | None type Msg = Start | ShowDialog DialogOf | CancelDialog 我按如下方式处理模型更新,效果很好 ShowDialog dialogOf -> c
type DialogOf =
SearchItems
| SearchingItems String
| None
type Msg =
Start
| ShowDialog DialogOf
| CancelDialog
我按如下方式处理模型更新,效果很好
ShowDialog dialogOf ->
case dialogOf of
SearchItems ->
( { model | dialogOf = SearchItems }, Cmd.none)
SearchingItems filter ->
( {model | context = CurrentContext filter }, Cmd.none )
None -> (model ,Cmd.none)
现在,当我触发事件时,我想使用过滤器(字符串)触发SearchingItems,这可以使用onClick按钮完成:
let searchWordButton item =
div [] [
button [ onClick (ShowDialog (SearchingItems item))] [text item]
]
现在我想为文本框启动onInput以过滤文本输入,但我找不到任何方法来使用隐式传递的值进行过滤-我正在尝试这样做(不起作用):
我意识到可能还有其他更好的方法来处理这个问题(比如),但我想知道是否有一种方法可以使用上面的链式联合类型通过onInput事件隐式传递字符串值
谢谢因为ShowDialogue是一种
(DialogueOf -> Msg)
但是onInput需要类型为的参数
(String -> Msg)
换句话说,onInput将字符串传递给消息(ShowDialogue),而不是传递给构造函数SearchingItems
我不相信有一种方法可以直接访问字符串(这将允许您直接将其传递给SearchingItems)。如果您想更深入地了解,您可以考虑使用()上的创建自定义事件侦听器,但我认为这不管用,而且似乎有点过分了
您最好使用两条不同的消息来捕获不同的用途:
type Msg
= Start
| ShowDialog DialogOf
| ShowDialogWithString String
| CancelDialog
更新功能:
ShowDialog dialogOf ->
( { model | dialogOf = SearchItems }, Cmd.none)
ShowDialogWithString filter ->
( {model | context = CurrentContext filter }, Cmd.none )
看法
你想在你的应用程序中添加什么功能来引导你走上这条道路?我需要在一个已经很忙的表单中添加多个对话框,我认为这可能是一个简化消息/更新的好方法,而不是有很多消息类型。我想我将不得不使用子模块。也许你应该后退一步,重新考虑用户体验。重新设计界面以减少表单上的噪音,并消除对多个对话框的需要。这可能不是一个需要用代码修复的问题。
ShowDialog dialogOf ->
( { model | dialogOf = SearchItems }, Cmd.none)
ShowDialogWithString filter ->
( {model | context = CurrentContext filter }, Cmd.none )
div []
[ input
[ value model.context.filter, onInput ShowDialogWithString ] []
]