嵌套形式导致 Haskell
Nested form result in Haskell
我有以下 handler/template 组合:
handler/automation.hs
data AutomationRequest = AutomationRequest {
arEnabled :: Bool
, arTemplate :: Text
, arSchedules :: Textarea
}
getAutomationR :: Handler Html
getAutomationR = do
(formWidget, formEnctype) <- generateFormPost form
defaultLayout $(widgetFile "automation")
form :: Form AutomationRequest
form extra = do
(enabledRes, enabledView) <- mreq checkBoxField "" Nothing
(templateRes, templateView) <- mreq textField (withPlaceholder "..." $ bfs (""::Text)) Nothing
(schedulesRes, schedulesView) <- mreq textareaField (withPlaceholder "..." $ bfs (""::Text)) Nothing
(_, submitView) <- mbootstrapSubmit $ BootstrapSubmit ("Save"::Text) ("btn-primary"::Text) []
let requestRes = AutomationRequest <$> enabledRes <*> templateRes <*> schedulesRes
widget = $(widgetFile "automation-form")
return (requestRes, widget)
templates/automation.hamlet
<form method=post role=form action=@{AutomationR} enctype=#{formEnctype}>
^{formWidget}
templates/automation-form.hamlet
#{extra}
<div .panel .panel-default>
<div .panel-heading>^{fvInput enabledView} ...
<div .panel-body>
^{fvInput templateView}
^{fvInput schedulesView}
^{fvInput submitView}
这按预期工作,但我需要额外的功能:
a) 我希望能够像这样嵌套数据结构:
data AutomationRequestCollection = AutomationRequestCollection {
arcItemAbc :: AutomationRequest
, arcItemDef :: AutomationRequest
... -- 10 Items
}
data AutomationRequest = AutomationRequest {
arEnabled :: Bool
, arTemplate :: Text
, arSchedules :: Textarea
}
我不知道如何将嵌套应用到 let requestRes = AutomationRequest <$> enabledRes <*> templateRes <*> schedulesRes
b) 为 itemAbc、itemDef、...重复使用 HTML 面板:
-- loop somehow
<div .panel .panel-default>
<div .panel-heading>^{fvInput enabledView} ...
<div .panel-body>
^{fvInput templateView}
^{fvInput schedulesView}
有什么想法可以将我推向正确的方向吗?
我不确定(b),但(a)应该是直截了当的应用组合,例如:
Foo <$> (Bar <$> baz <*> bin) <*> qux
如果把它分解成多个函数也能更容易看出来:
bar = Bar <$> baz <*> bin
foo = Foo <$> bar <*> qux
我有以下 handler/template 组合:
handler/automation.hs
data AutomationRequest = AutomationRequest {
arEnabled :: Bool
, arTemplate :: Text
, arSchedules :: Textarea
}
getAutomationR :: Handler Html
getAutomationR = do
(formWidget, formEnctype) <- generateFormPost form
defaultLayout $(widgetFile "automation")
form :: Form AutomationRequest
form extra = do
(enabledRes, enabledView) <- mreq checkBoxField "" Nothing
(templateRes, templateView) <- mreq textField (withPlaceholder "..." $ bfs (""::Text)) Nothing
(schedulesRes, schedulesView) <- mreq textareaField (withPlaceholder "..." $ bfs (""::Text)) Nothing
(_, submitView) <- mbootstrapSubmit $ BootstrapSubmit ("Save"::Text) ("btn-primary"::Text) []
let requestRes = AutomationRequest <$> enabledRes <*> templateRes <*> schedulesRes
widget = $(widgetFile "automation-form")
return (requestRes, widget)
templates/automation.hamlet
<form method=post role=form action=@{AutomationR} enctype=#{formEnctype}>
^{formWidget}
templates/automation-form.hamlet
#{extra}
<div .panel .panel-default>
<div .panel-heading>^{fvInput enabledView} ...
<div .panel-body>
^{fvInput templateView}
^{fvInput schedulesView}
^{fvInput submitView}
这按预期工作,但我需要额外的功能:
a) 我希望能够像这样嵌套数据结构:
data AutomationRequestCollection = AutomationRequestCollection {
arcItemAbc :: AutomationRequest
, arcItemDef :: AutomationRequest
... -- 10 Items
}
data AutomationRequest = AutomationRequest {
arEnabled :: Bool
, arTemplate :: Text
, arSchedules :: Textarea
}
我不知道如何将嵌套应用到 let requestRes = AutomationRequest <$> enabledRes <*> templateRes <*> schedulesRes
b) 为 itemAbc、itemDef、...重复使用 HTML 面板:
-- loop somehow
<div .panel .panel-default>
<div .panel-heading>^{fvInput enabledView} ...
<div .panel-body>
^{fvInput templateView}
^{fvInput schedulesView}
有什么想法可以将我推向正确的方向吗?
我不确定(b),但(a)应该是直截了当的应用组合,例如:
Foo <$> (Bar <$> baz <*> bin) <*> qux
如果把它分解成多个函数也能更容易看出来:
bar = Bar <$> baz <*> bin
foo = Foo <$> bar <*> qux