2013-04-26 8 views
5

Sto cercando di capire le giunzioni compilate e come usarle con i moduli del funtore digestivo. Qualcuno ha qualche esempio di codice?Snap Framework: giunzioni compilate e moduli di elaborazione con funtori digestivi

+0

Quello che hai ci sembra buono. Hai una domanda più specifica? – mightybyte

+0

Grazie mightybyte. Non sono stato in grado di trovare esempi di moduli dig-func usati con giunzioni compilate e volevo solo alcune indicazioni su come usarli insieme e sfruttare i guadagni di efficienza. –

+0

Questo è un bell'esempio. Forse potresti cambiare la tua domanda in modo che richieda solo un esempio? Quindi rispondi tu stesso con il codice che hai qui in modo che venga visualizzato come una domanda risposta. – mightybyte

risposta

3

i seguenti lavori per l'elaborazione del modulo di giunzione compilato ...

bookFormSplice :: C.Splice (Handler App App) 
bookFormSplice = formSplice $ do 
    (view,result) <- DFS.runForm "bookForm" bookForm -- runForm is in Text.Digestive.Snap 
    case result of Just x -> redirect "/" --valid result, redirect to the home page 
             --can also insert into DB here 
       Nothing -> return view --no result or invalid form data, 
             --return the view and render the form page 

applicazioni aggiuntive, i dati, il rendering di codice ...

data Book = Book { title :: T.Text 
       , description :: T.Text } 


bookForm :: Monad m => Form T.Text m Book 
bookForm = check "Cannot be blank" notBlank $ Book 
    <$> "title" .: text (Nothing) 
    <*> "description" .: text Nothing 
    where 
     notBlank (Book t d) = t /= "" && d /= "" 




handleNewBook :: Handler App App() 
handleNewBook = cRender "newBook" 



routes :: [(ByteString, Handler App App())] 
routes = [ ("/login", with auth handleLoginSubmit) 
    , ("/logout", with auth handleLogout) 
    , ("/new_user", with auth handleNewUser) 
    , ("/newBook", handleNewBook) 
    , ("",   serveDirectory "static") 
    ] 


app :: SnapletInit App App 
app = makeSnaplet "app" "An snaplet example application." Nothing $ do 
h <- nestSnaplet "" heist $ heistInit "templates" 
s <- nestSnaplet "sess" sess $ 
     initCookieSessionManager "site_key.txt" "sess" (Just 3600) 
a <- nestSnaplet "auth" auth $ 
     initJsonFileAuthManager defAuthSettings sess "users.json" 

let config = mempty { hcCompiledSplices = [("bookForm", bookFormSplice)]} 
addConfig h config 
addRoutes routes 
addAuthSplices auth 
return $ App h s a 

Il modello "newBook"

New Book Entry: 
<br> 

<bookForm action="/newBook"> 

<dfChildErrorList ref="" /> 

<dfInputText ref="title"/> 
<dfInputText ref="description"/> 
<dfInputSubmit value="submit"/> 
</bookForm>