在客户端/服务器编程中使用3penny-gui /响应 [英] using threepenny-gui/reactive in client/server programming
问题描述
我想弄清楚如何使用Haskell 以及它的< a href =http://hackage.haskell.org/package/threepenny-gui-0.4.2.0/docs/Reactive-Threepenny.html =nofollow> reactive 功能来编写程序,让
$ b
- 用户从列表框中选择一个项目
- 将选择内容发送到外部服务器 li>
- 从服务器获取结果列表
- 使用结果填充列表框
- 重复 >
看来我需要使用 Handler
, newEvent
和寄存器
来完成上述操作。如果有人可以指向我现有的代码,这样做会很好。
我找到的最接近的是 GameThing.hs
在threepenny-gui samples
目录中(但它不使用 register
)。
UPDATE:
我在问是否应该使用 Handler
, newEvent
和 register
---,如果有的话,这些函数的一些说明或示例。
另外,要清楚的是,大图是:
浏览器 - > threepenny-gui(在本地主机上) - >后端服务器(网络上的任何地方)
< - < -
单词,我需要做一些IO(基于用户选择),然后显示该IO的结果。
更新:这里是我的解决方案(基于@Taldykin的回复) : https://github.com/haroldcarr/rdf-triple-浏览器/ tree / master / haskell / src
这是一段代码。
{ - #LANGUAGE LambdaCase# - }
import Graphics.UI.Threepenny作为UI
main :: IO()
main = do
(evFillList,doFillList)< - newEvent
initialList< - valuesSupply&
behFillList< - stepper initialList evFillList
startGUI defaultConfig $ \win - > do
list< - ul
sel< - listBox
behFillList
(纯粹无)
(纯$ \it - > UI.span#set文本)
getBody win#+ [grid [[element list,element sel]]]
setFocus $ getElement sel
on selectionChange(getElement sel)$ \ case
Nothing - > return()
只是ix - >做
项目< - currentValue behFillList
让它=项目! ix
liftIO $ valuesSupply it>> = doFillList
element list#+ [li#set html it]
setFocus $ getElement sel
valuesSupply :: String - > IO [String]
valuesSupply x = return [x ++ show i | i< - [0..9]]
I am trying to figure out how to use Haskell threepenny-gui with its reactive functionality to write a program that lets
- the user select an item from a listBox
- send the selection to an external server
- get back a list of results from the server
- populate the listBox with the results
- repeat
It seems I will need to use Handler
, newEvent
and register
to do the above. If someone could point me to some existing code that does something like the above that would be great.
The closest I have found is GameThing.hs
in the threepenny-gui samples
directory (but it doesn't use register
).
UPDATE:
I am asking whether I should be using Handler
, newEvent
and register
--- and, if so, some clarification or example of those functions.
Also, to be clear, the big picture is:
browser --> threepenny-gui (on localhost) --> backend server (anywhere on network)
<-- <--
In other words, I need to do some IO (based on user selection) then display the results of that IO.
UPDATE: here is my solution (based on @Taldykin's response): https://github.com/haroldcarr/rdf-triple-browser/tree/master/haskell/src
Here is a piece of code. I will add description a bit later.
{-# LANGUAGE LambdaCase #-}
import Graphics.UI.Threepenny as UI
main :: IO ()
main = do
(evFillList, doFillList) <- newEvent
initialList <- valuesSupply ""
behFillList <- stepper initialList evFillList
startGUI defaultConfig $ \win -> do
list <- ul
sel <- listBox
behFillList
(pure Nothing)
(pure $ \it -> UI.span # set text it)
getBody win #+ [grid [[element list, element sel]]]
setFocus $ getElement sel
on selectionChange (getElement sel) $ \case
Nothing -> return ()
Just ix -> do
items <- currentValue behFillList
let it = items !! ix
liftIO $ valuesSupply it >>= doFillList
element list #+ [li # set html it]
setFocus $ getElement sel
valuesSupply :: String -> IO [String]
valuesSupply x = return [x ++ show i | i <- [0..9]]
这篇关于在客户端/服务器编程中使用3penny-gui /响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!