简体   繁体   English

如何在purescript-halogen中组合有效的事件处理程序和自定义EventUpdates?

[英]How do I combine effectful event handlers and custom EventUpdates in purescript-halogen?

In my custom Halogen/Purescript project I follow the pattern from the AJAX Example where I split my actions up into pure Input s and effectful Request s. 在我的自定义Halogen / Purescript项目中,我遵循AJAX示例中的模式,我将我的操作分解为纯Input和有效Request

I want to change my event handler to use the preventDefault behavior, but don't understand what consequences for the type of the UI function this entails. 我想更改我的事件处理程序以使用preventDefault行为,但不明白这会对UI函数的类型产生什么影响。

I made the same changes to the AJAX Example by changing the event handler the following way: 我通过以下方式更改事件处理程序,对AJAX示例进行了相同的更改:

Before: 之前:

H.button [ A.classes [B.btn, B.btnPrimary]
         , A.disabled busy
         , A.onclick (\_ -> pure (handler code))
         ] [ H.text "Compile" ]

After: 后:

H.a [ A.classes [B.btn, B.btnPrimary]
    , A.href "#compile"
    , A.disabled busy
    , A.onclick (\_ -> E.preventDefault $> pure (handler code))
    ] [ H.text "Compile" ]

(Full diff available here ) (完全差异在这里可用)

I end up with this type error: 我最终得到这种类型的错误:

Cannot unify type
    Example.Ajax.Input
    with type
        Halogen.HTML.Events.Monad.Event Halogen.HalogenEffects<(http ::
        Example.Ajax.HTTP | u32519)> Example.Ajax.Input

At this point, I'm a bit lost whether I would need to adjust the type signature of the UI function or I apply the preventDefault modifier the wrong way. 此时,我有点迷失是否需要调整UI函数的类型签名,或者我以错误的方式应用preventDefault修饰符。

The type of $> looks like: $>的类型如下:

($>) :: forall a. EventHandler a -> b -> EventHandler b

The type of pure looks like: pure的类型看起来像:

pure :: forall a. a -> EventHandler a

So the problem is by using both together, you're making a type which looks like this: 所以问题是通过一起使用,你正在制作一个如下所示的类型:

EventHandler a -> EventHandler b -> EventHandler (EventHandler b)

But you don't want that, you just want an EventHandler b , where b is the E.Event type of handler code . 但是你不希望这样,你只需要一个EventHandler b ,其中bE.Event类型的handler code

The best solution is to just not use pure : 最好的解决方案是不使用pure

E.preventDefault $> handler code

If sometime you do have two EventHandler values you want to use together like this, the function to use is *> instead of $> . 如果有时你想要像这样使用两个EventHandler值,那么使用的函数是*>而不是$>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM