2

我有以下处理程序/模板组合:

处理程序/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)

模板/automation.hamlet

<form method=post role=form action=@{AutomationR} enctype=#{formEnctype}>
    ^{formWidget}

模板/自动化-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}

有什么想法可以将我推向正确的方向吗?

4

1 回答 1

1

我不确定(b),但(a)应该是简单的应用组合,例如:

Foo <$> (Bar <$> baz <*> bin) <*> qux

如果您将其分解为多个函数,也可以更容易地查看:

bar = Bar <$> baz <*> bin
foo = Foo <$> bar <*> qux
于 2015-07-22T13:27:47.843 回答