4

我想要这样的东西:

on UI.keydown textfield $ \c -> when (c == 13) $ void $ do
    trigger UI.click button

也就是说,有没有像trigger我刚刚插入的功能一样的东西?

4

1 回答 1

1

为了Enter像处理按钮点击一样处理按键,您不需要逐字触发点击。相反,您需要一个在按键或点击发生时触发的事件。最简单的方法是通过unionWith. 使用该组合器和其他一些Reactive.Threepenny组合器,代码可能如下所示:

-- I'm giving separate names to the events for extra clarity.
let eClick = UI.click button
    eEnter = void $ filterE (== 13) (UI.keydown textfield)
    -- This event is fired whenever `eClick` or `eEnter` happen.
    eGo = unionWith const eClick eEnter

然后,您只需以处理 click 事件的方式处理eGo, using 。onEvent

请注意,根据您问题中伪代码的精神,另一种解决方案是定义eGovia newEvent,然后使用触发函数在onclick 和 keypress 的处理程序中触发事件:

-- Assuming you are in an `UI` do-block, thus the `liftIO`.
(eGo, fireGo) <- liftIO newEvent
on UI.click button $ \_ -> fireGo ()
on UI.keydown textfield $ \c -> when (c == 13) (fireGo ())
-- Alternatively, you might define an `eEnter` with `filterE`,
-- as in the first example, instead of using `on` and `when`. 

这不像第一个解决方案那样好,因为它有点混乱,并且在 FRP 代码中运行起来也不太顺畅。我不会推荐它,除非你根本不使用 FRP 组合器。

于 2015-12-03T16:56:09.403 回答