[英]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
,其中b
是E.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.