Last active
March 17, 2018 09:18
-
-
Save blargg/a945a9046b08e50b467c146570e5d4e8 to your computer and use it in GitHub Desktop.
Using javascript function with Reflex and JSaddle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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