Skip to content

Instantly share code, notes, and snippets.

@blargg
Last active March 17, 2018 09:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save blargg/a945a9046b08e50b467c146570e5d4e8 to your computer and use it in GitHub Desktop.
Save blargg/a945a9046b08e50b467c146570e5d4e8 to your computer and use it in GitHub Desktop.
Using javascript function with Reflex and JSaddle
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ExtendedDefaultRules #-}
import Reflex.Dom
import Control.Monad.IO.Class
import Control.Monad (void)
import Data.Text (Text)
import Language.Javascript.JSaddle
import Control.Lens ((^.))
main :: IO ()
main = mainWidget localStorageTest
saveLocal :: Text -> JSM ()
saveLocal msg = do
jsg "window"
^. js "localStorage"
^. jss "saveLocation" [msg]
return ()
getLocal :: JSM (Maybe Text)
getLocal = do
jsv <- jsg "window"
^. js "localStorage"
^. js "saveLocation"
liftJSM (fromJSVal jsv)
localStorageTest :: MonadWidget t m => m ()
localStorageTest = do
ctx <- unJSContextSingleton <$> askJSContext
el "div" $ do
t <- textInput def
set <- button "set value"
performEvent_ $ ffor (tag (current $ value t) set) $ liftIO . runJSaddle ctx . saveLocal
el "div" $ do
get <- button "get value"
val <- performEvent $ ffor get $ \_ -> liftIO . runJSaddle ctx $ getLocal
display =<< holdDyn Nothing val
return ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment