[英]Structuring a dynamic list of reflex-dom widgets/events according to numeric user input
I'm trying to create a dynamic list of widgets with the number of widgets determined by a numeric value from user input. 我正在尝试创建一个动态的小部件列表,其中小部件的数量由用户输入的数值确定。 Furthermore, each widget returns a click event.
此外,每个小部件都返回一个单击事件。 Here's what I'm using to get the user input:
这是我用来获取用户输入的内容:
settings :: MonadWidget t m => m (Dynamic t (Maybe Int))
Then I use this to generate a list of random number generators (The fact that these are values of RandomGen
is not significant. They're just used for the content of each element, not the number of elements).: 然后我用它来生成一个随机数生成器列表(事实上这些是
RandomGen
值并不重要。它们只用于每个元素的内容, 而不是元素的数量):
split' :: RandomGen g => Int -> g -> [g]
-- ...
gs <- mapDyn (maybe (split' 1 g) (flip split' g)) =<< settings
Now I have gs :: (MonadWidget tm, RandomGen g) => Dynamic t [g]
. 现在我有
gs :: (MonadWidget tm, RandomGen g) => Dynamic t [g]
。 One g
for each widget. 每个小部件一个
g
。 These widgets return Event
values so I'll need to combine them (ie leftmost
) then use that value with foldDyn
somewhere. 这些小部件返回
Event
值,因此我需要将它们组合在一起(即leftmost
),然后将该值与foldDyn
一起使用。
go :: (MonadWidget t m, Random g) => g -> m (Event t MyType)
-- ...
clicked <- el "div" $ do
-- omg
xs <- simpleList gs go
-- would like to eventually do this
dynEvent <- mapDyn leftmost xs
return $ switch (current dynEvent)
But so far I end up with xs :: Dynamic t [Dynamic t (m (Event t MyType))]
. 但到目前为止,我最终得到了
xs :: Dynamic t [Dynamic t (m (Event t MyType))]
。
I think what I really need is to somehow make xs :: MonadWidget tm => Dynamic t [Event t MyType]
instead but having some trouble getting there even with other functions aside from simpleList
. 我认为我真正需要的是以某种方式使
xs :: MonadWidget tm => Dynamic t [Event t MyType]
而是在使用除simpleList
之外的其他函数时遇到一些麻烦。
Your problem is that simpleList takes a Dynamic t [g]
and (Dynamic tg -> ma)
. 你的问题是simpleList采用
Dynamic t [g]
和(Dynamic tg -> ma)
。 However, your go is g -> m (Event t MyType). 但是,你的go是g - > m(事件t MyType)。 So you need to create a better go:
所以你需要创造一个更好的去:
go2 :: (MonadWidget t m, RandomGen g) => Dynamic t g -> m (Event t MyType)
go2 gDyn = do
mapped <- mapDyn go gDyn
dyned <- dyn mapped
held <- hold never dyned
return (switch held)
Once you have this, it should be easier as simpleList gs go2
will return m (Dynamic t [Event t MyType])
and you should be able to mapDyn
leftmost
over it. 一旦你有了这个,它应该更容易,因为
simpleList gs go2
将返回m (Dynamic t [Event t MyType])
,你应该能够在它mapDyn
leftmost
mapDyn
。
This is not the most elegant solution, but that's the best I was able to find when I was trying something similar. 这不是最优雅的解决方案,但这是我在尝试类似的东西时能够找到的最好的解决方案。 I'm sure it could be extracted into some helper function.
我敢肯定它可以被提取到一些辅助函数中。
Note that I don't have a compiler with me, and typechecking this in my head is quite difficult, so if it doesn't work, write a comment. 请注意,我没有编译器,并且在我的头脑中进行类型检查是非常困难的,所以如果它不起作用,请写一个注释。 I'll have a look when I'm home with a compiler.
当我在家里使用编译器时,我会看看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.