-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Anler Hernandez Peral
committed
Feb 10, 2016
1 parent
0296728
commit 4035614
Showing
9 changed files
with
329 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
This is the first example showing how to handle external effects. Basically the signature for your `init` and `update` function changes: | ||
|
||
* `init` returns the tuple `[model, effect]`. | ||
* `update` returns the tuple `[model, effect]`. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import StartApp from 'olmo/start-app'; | ||
import Signal from 'olmo/signal'; | ||
import Task from 'olmo/task'; | ||
|
||
import snabbdom from 'snabbdom'; | ||
import snabbdomProps from 'snabbdom/modules/props'; | ||
import snabbdomEvents from 'snabbdom/modules/eventlisteners'; | ||
|
||
import RandomGif from './random-gif'; | ||
|
||
|
||
const patch = snabbdom.init([snabbdomProps, snabbdomEvents]); | ||
|
||
function render([oldHtml, newHtml]) { | ||
return Task.of(() => patch(oldHtml, newHtml)); | ||
} | ||
|
||
export default function App() { | ||
var {html, model, tasks} = StartApp.App({ | ||
init: RandomGif.init(), | ||
update: RandomGif.update, | ||
view: RandomGif.view, | ||
inputs: [] | ||
}); | ||
|
||
return { | ||
html, | ||
model, | ||
// tasks perform side-effects | ||
tasks: Signal.merge( | ||
tasks, | ||
// rendering is just a side-effect | ||
html | ||
.startWith(document.getElementById('root')) | ||
.pairwise() | ||
.flatMap(render) | ||
) | ||
}; | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<!doctype html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
<meta name="description" content=""> | ||
<meta name="viewport" content="width=device-width"> | ||
|
||
<title>Elm Architecture Tutorial</title> | ||
<style> | ||
html,head,body { padding:0; margin:0; } | ||
body { font-family: calibri, helvetica, arial, sans-serif; } | ||
</style> | ||
</head> | ||
|
||
<body> | ||
<div id="root"></div> | ||
|
||
<script src="bundle.js"></script> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { runApp } from 'olmo/start-app'; | ||
import App from './app'; | ||
|
||
runApp(App()); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { html } from 'snabbdom-jsx'; | ||
|
||
import Http from 'olmo/http'; | ||
import Task from 'olmo/task'; | ||
import Events from 'olmo/html-events'; | ||
import Effects from 'olmo/effects'; | ||
import ActionType from 'olmo/actions'; | ||
import Json from 'olmo/json'; | ||
|
||
|
||
// init | ||
export function init(topic='funny cats') { | ||
const model = {topic, url: 'assets/waiting.gif'}; | ||
const eff = getRandomGif(topic); | ||
return [model, eff]; | ||
} | ||
|
||
function getRandomGif(topic) { | ||
return Effects.fromTask( | ||
Http.get(decodeImageUrl, getRandomUrl(topic)), | ||
Action.NewGif, | ||
Action.GifError | ||
); | ||
} | ||
|
||
function getRandomUrl(topic) { | ||
return Http.url('http://api.giphy.com/v1/gifs/random', [ | ||
['api_key', 'dc6zaTOxFJmzC'], | ||
['tag', topic] | ||
]); | ||
} | ||
|
||
function decodeImageUrl(resp) { | ||
return Json.at(['data', 'image_url'], Json.string, resp.value); | ||
} | ||
|
||
|
||
// action | ||
const Action = ActionType({ | ||
RequestMore: [], | ||
NewGif: ['url'], | ||
GifError: ['error'] | ||
}); | ||
|
||
|
||
// update | ||
export const update = Action.case('RandomGif', { | ||
RequestMore: (action, model) => { | ||
return [model, getRandomGif(model.topic)]; | ||
}, | ||
|
||
NewGif: (action, model) => { | ||
const newModel = {...model, url: action.url}; | ||
return [newModel, Effects.none()]; | ||
}, | ||
|
||
GifError: (action, model) => { | ||
console.log('I got error ', action); | ||
return [model, Effects.none()]; | ||
} | ||
}); | ||
|
||
|
||
// view | ||
function view(address, model) { | ||
console.log(model.url); | ||
return ( | ||
<div> | ||
<h1>{model.topic}</h1> | ||
<p><button on-click={Events.message(address, Action.RequestMore())}>More Please!</button></p> | ||
<img src={model.url}/> | ||
</div> | ||
); | ||
} | ||
|
||
|
||
export default {init, update, view}; |
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