Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support shared data types & (ad-hoc) polymorphic serialization.

  • Loading branch information...
commit e47590e826341af848fd1fc7fbfaeea07eeed50c 1 parent 7468efe
@chrisdone chrisdone authored
View
1  .gitignore
@@ -5,3 +5,4 @@ example/snaplets/fay/js
*.hi
*.o
cabal-dev
+TAGS
View
2  example/example.cabal
@@ -15,7 +15,7 @@ Flag development
Default: False
Executable example
- hs-source-dirs: src
+ hs-source-dirs: src snaplets/fay/src
main-is: Main.hs
Build-depends:
View
10 example/snaplets/fay/src/Application/SharedTypes.hs
@@ -0,0 +1,10 @@
+{-# LANGUAGE NoImplicitPrelude #-}
+
+module Application.SharedTypes where
+
+import Language.Fay.Prelude
+import Language.Fay.FFI
+
+data CTR = CTR { time :: String }
+ deriving (Show)
+instance Foreign CTR
View
15 example/snaplets/fay/src/Index.hs
@@ -7,6 +7,7 @@ module Index where
import Language.Fay.FFI
import Language.Fay.Prelude
+import Application.SharedTypes
import Dom
main :: Fay ()
@@ -25,17 +26,11 @@ onload = do
return ()
-data CTR = CTR { time :: String }
-instance Foreign CTR
-
currentTime :: Fay ()
currentTime = do
- ajaxJson "/ajax/current-time" handleResponse
-
-handleResponse :: CTR -> Fay ()
-handleResponse (CTR time) = do
- el <- byId "current-time"
- setInnerHtml el time
+ ajaxJson "/ajax/current-time" $ \(CTR time) -> do
+ el <- byId "current-time"
+ setInnerHtml el time
-ajaxJson :: String -> (CTR -> Fay ()) -> Fay ()
+ajaxJson :: Foreign a => String -> (a -> Fay ()) -> Fay ()
ajaxJson = ffi "jQuery.ajax(%1, { success : %2 })"
View
24 example/src/Site.hs
@@ -9,24 +9,26 @@ module Site
) where
------------------------------------------------------------------------------
-import Control.Monad.Trans
-import Data.Aeson
-import Data.ByteString (ByteString)
-import Data.Time.Clock
-import Snap.Core
-import Snap.Snaplet
-import Snap.Snaplet.Heist
-import Snap.Snaplet.Fay
-import Snap.Util.FileServe
+import Control.Monad.Trans
+import Data.Aeson
+import Data.ByteString (ByteString)
+import Data.Time.Clock
+import Language.Fay.Show
+import Snap.Core
+import Snap.Snaplet
+import Snap.Snaplet.Fay
+import Snap.Snaplet.Heist
+import Snap.Util.FileServe
------------------------------------------------------------------------------
-import Application
+import Application
+import Application.SharedTypes
currentTimeAjax :: AppHandler ()
currentTimeAjax = do
time <- liftIO getCurrentTime
modifyResponse . setContentType $ "text/json;charset=utf-8"
- writeLBS $ encode . toJSON $ object ["instance" .= ("CTR" :: ByteString), "time" .= show time]
+ writeLBS $ encode $ showToFay (CTR (show time))
------------------------------------------------------------------------------
View
2  snaplet-fay.cabal
@@ -46,7 +46,7 @@ library
configurator == 0.2.*,
data-default == 0.5.*,
directory == 1.1.*,
- fay == 0.6.*,
+ fay == 0.6.1.*,
filepath == 1.3.*,
mtl == 2.1.*,
snap == 0.9.*,

0 comments on commit e47590e

Please sign in to comment.
Something went wrong with that request. Please try again.