Created
March 28, 2016 16:09
-
-
Save anonymous/cf2386a9030f66ae98d3 to your computer and use it in GitHub Desktop.
MotorcycleJS http example // source http://jsbin.com/vefotux
This file contains hidden or 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> | |
<meta name="description" content="http example"> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<script src="https://wzrd.in/standalone/most"></script> | |
<script src="https://wzrd.in/standalone/@motorcycle%2Fcore"></script> | |
<script src="https://gist.githubusercontent.com/Risto-Stevcev/2f407cbcc1ce33074237/raw/9ed5867704a5f4be53e34e450709706820d342cf/dom.js"></script> | |
<script src="https://gist.githubusercontent.com/Risto-Stevcev/62881c9b9a60931f1bc9/raw/54f6e2ae704f8a08d1d1a80b665c154263a500cb/http.js"></script> | |
<title>MotorcycleJS</title> | |
<div id="app"></div> <!-- Our container --> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
/* jshint esnext: true */ | |
'use strict'; | |
var _DOM = DOM; | |
var div = _DOM.div; | |
var button = _DOM.button; | |
var h1 = _DOM.h1; | |
var h4 = _DOM.h4; | |
var a = _DOM.a; | |
var makeDOMDriver = _DOM.makeDOMDriver; | |
var _HTTP = HTTP; | |
var makeHTTPDriver = _HTTP.makeHTTPDriver; | |
function main(sources) { | |
var USERS_URL = 'http://jsonplaceholder.typicode.com/users/'; | |
var getRandomUser$ = sources.DOM.select('.get-random').events('click').map(function () { | |
var randomNum = Math.round(Math.random() * 9) + 1; | |
return { | |
url: USERS_URL + String(randomNum), | |
method: 'GET' | |
}; | |
}); | |
var user$ = sources.HTTP.filter(function (res$) { | |
return res$.request.url.indexOf(USERS_URL) === 0; | |
}).join().map(function (res) { | |
return res.body; | |
}).startWith(null); | |
var vtree$ = user$.map(function (user) { | |
return div('.users', [button('.get-random', 'Get random user'), user === null ? null : div('.user-details', [h1('.user-name', user.name), h4('.user-email', user.email), a('.user-website', { props: { href: user.website } }, user.website)])]); | |
}); | |
var sinks = { | |
DOM: vtree$, | |
HTTP: getRandomUser$ | |
}; | |
return sinks; | |
} | |
motorcycle2Fcore.run(main, { | |
DOM: makeDOMDriver('#app'), | |
HTTP: makeHTTPDriver() | |
}); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">/* jshint esnext: true */ | |
const {div, button, h1, h4, a, makeDOMDriver} = DOM; | |
const {makeHTTPDriver} = HTTP; | |
function main(sources) { | |
const USERS_URL = 'http://jsonplaceholder.typicode.com/users/'; | |
const getRandomUser$ = sources.DOM.select('.get-random').events('click') | |
.map(() => { | |
const randomNum = Math.round(Math.random()*9)+1; | |
return { | |
url: USERS_URL + String(randomNum), | |
method: 'GET' | |
}; | |
}); | |
const user$ = sources.HTTP | |
.filter(res$ => res$.request.url.indexOf(USERS_URL) === 0) | |
.join() | |
.map(res => res.body) | |
.startWith(null); | |
const vtree$ = user$.map(user => | |
div('.users', [ | |
button('.get-random', 'Get random user'), | |
user === null ? null : div('.user-details', [ | |
h1('.user-name', user.name), | |
h4('.user-email', user.email), | |
a('.user-website', {props: {href: user.website}}, user.website) | |
]) | |
]) | |
); | |
const sinks = { | |
DOM: vtree$, | |
HTTP: getRandomUser$ | |
}; | |
return sinks; | |
} | |
motorcycle2Fcore.run(main, { | |
DOM: makeDOMDriver('#app'), | |
HTTP: makeHTTPDriver() | |
});</script></body> | |
</html> |
This file contains hidden or 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
/* jshint esnext: true */ | |
'use strict'; | |
var _DOM = DOM; | |
var div = _DOM.div; | |
var button = _DOM.button; | |
var h1 = _DOM.h1; | |
var h4 = _DOM.h4; | |
var a = _DOM.a; | |
var makeDOMDriver = _DOM.makeDOMDriver; | |
var _HTTP = HTTP; | |
var makeHTTPDriver = _HTTP.makeHTTPDriver; | |
function main(sources) { | |
var USERS_URL = 'http://jsonplaceholder.typicode.com/users/'; | |
var getRandomUser$ = sources.DOM.select('.get-random').events('click').map(function () { | |
var randomNum = Math.round(Math.random() * 9) + 1; | |
return { | |
url: USERS_URL + String(randomNum), | |
method: 'GET' | |
}; | |
}); | |
var user$ = sources.HTTP.filter(function (res$) { | |
return res$.request.url.indexOf(USERS_URL) === 0; | |
}).join().map(function (res) { | |
return res.body; | |
}).startWith(null); | |
var vtree$ = user$.map(function (user) { | |
return div('.users', [button('.get-random', 'Get random user'), user === null ? null : div('.user-details', [h1('.user-name', user.name), h4('.user-email', user.email), a('.user-website', { props: { href: user.website } }, user.website)])]); | |
}); | |
var sinks = { | |
DOM: vtree$, | |
HTTP: getRandomUser$ | |
}; | |
return sinks; | |
} | |
motorcycle2Fcore.run(main, { | |
DOM: makeDOMDriver('#app'), | |
HTTP: makeHTTPDriver() | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment