-
-
Save anonymous/64ec4a87bd5a713ac058 to your computer and use it in GitHub Desktop.
Integrating the XHR module with purescript-thermite
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
module Main where | |
import Debug.Trace | |
import Network.XHR | |
import qualified Thermite as T | |
import qualified Thermite.Html as T | |
import qualified Thermite.Html.Elements as T | |
import qualified Thermite.Action as T | |
import qualified Thermite.Types as T | |
type State = { message:: String } | |
data Action = LoadData | |
spec :: T.Spec (T.Action _ State) State Unit Action | |
spec = T.simpleSpec initialState performAction render | |
# T.componentWillMount LoadData | |
render :: T.Render State Unit Action | |
render ctx st _ = T.div' [ T.text st.message ] | |
loadSetState :: forall eff. (State -> EffAjax (trace :: Trace | eff) Unit) -> EffAjax (trace:: Trace | eff) Unit | |
loadSetState f = do | |
get defaultAjaxOptions | |
{ onReadyStateChange = onSuccess $ \response -> do | |
txt <- getResponseText response | |
f { message: txt } | |
} "/api/values" {} | |
return unit | |
performAction :: T.PerformAction Unit Action (T.Action _ State) | |
performAction _ LoadData = T.asyncSetState loadSetState | |
initialState :: State | |
initialState = { message: "Oh, hai" } | |
main = do | |
let cl = T.createClass spec | |
T.render cl unit | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment