2015-07-21 69 views
2

我有以下處理/模板組合:嵌套形式,結果在Haskell

處理器/ 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 [email protected]{AutomationR} enctype=#{formEnctype}> 
    ^{formWidget} 

模板/ automation-form.hamlet

#{extra} 

<div .panel .panel-default> 
    <div .panel-heading>^{fvInput enabledView} ... 
    <div .panel-body> 
     ^{fvInput templateView} 
     ^{fvInput schedulesView} 

^{fvInput submitView} 

可正常工作,但我想更多的功能:

一)我希望能夠嵌套的數據結構,如:

data AutomationRequestCollection = AutomationRequestCollection { 
    arcItemAbc :: AutomationRequest 
    , arcItemDef :: AutomationRequest 
    ... -- 10 Items 

}

data AutomationRequest = AutomationRequest { 
    arEnabled :: Bool 
    , arTemplate :: Text 
    , arSchedules :: Textarea 
} 

我不知道如何應用嵌套到let requestRes = AutomationRequest <$> enabledRes <*> templateRes <*> schedulesRes

二)回用於itemAbc,itemDef的HTML面板...:

-- loop somehow 
<div .panel .panel-default> 
    <div .panel-heading>^{fvInput enabledView} ... 
    <div .panel-body> 
     ^{fvInput templateView} 
     ^{fvInput schedulesView} 

,可以把我推到正確的方向上沒有任何想法?

回答

1

我不知道(B),但(一)應當是簡單的應用型組成,如:

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

它也可以更容易地看到,如果你打破了這種成多種功能:

bar = Bar <$> baz <*> bin 
foo = Foo <$> bar <*> qux 
+0

很酷。謝謝。在某種程度上可以使用具有適用風格的記錄語法? '''AutomationRequestCollection {arcItemAbc = ...,arcItemDef = ...}''' – Alexander

+0

不幸的不是。 –