Last active
August 29, 2015 14:18
-
-
Save bbqbaron/3b25cb03573a804a913b to your computer and use it in GitHub Desktop.
Elm History?
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
<!DOCTYPE html> | |
<html> | |
<head></head> | |
<body> | |
<div id="elm"><!-- We will insert the elm app here --></div> | |
<script src="elm.js"></script> | |
<script type="text/javascript"> | |
var div = document.getElementById('elm'); | |
// Embed the elm app into a div and pass the initial location | |
var p = document.location.pathname; | |
var elmApp = Elm.fullscreen(Elm.Test, { | |
initialLocation: p, | |
pops: "" | |
}); | |
elmApp.ports.pushes.subscribe(function(x) { | |
window.history.pushState({}, '', x); | |
}); | |
window.onpopstate = function() { | |
elmApp.ports.pops.send(document.location.pathname); | |
} | |
</script> | |
</div> | |
</body> | |
</html> |
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 Test where | |
import Html(..) | |
import Html.Attributes(..) | |
import Html.Events(..) | |
import List(..) | |
import Maybe(..) | |
import Signal(..) | |
port initialLocation : String | |
port pushes : Signal String | |
port pushes = actionToUrl <~ (subscribe actions) | |
port pops : Signal String | |
type Action = Idle | Edit Int | Index | |
type alias Model = { | |
url : String, | |
editing : Maybe Int | |
} | |
urlToAction : String -> Action | |
urlToAction u = case u of | |
"/article/33/" -> Edit 33 | |
otherwise -> Index | |
actionToUrl : Action -> String | |
actionToUrl a = case a of | |
Edit i -> "/article/33/" | |
otherwise -> "/article/" | |
init : Model | |
init = {url=initialLocation, editing=Nothing} | |
update : Action -> Model -> Model | |
update a m = case a of | |
Edit i -> {m|editing <- Just i} | |
Index -> {m|editing <- Nothing} | |
otherwise -> m | |
actions : Channel Action | |
actions = channel Idle | |
state : Signal Model | |
state = foldp update init (merge (subscribe actions) (urlToAction <~ pops)) | |
view : Model -> Html | |
view m = div [] [ | |
case m.editing of | |
Just int -> text "editing!" | |
Nothing -> text "index!", | |
button [onClick (send actions (Edit 33))] [text "edit"], | |
button [onClick (send actions Index)] [text "index"] | |
] | |
main : Signal Html | |
main = view <~ state |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment