Created
February 5, 2014 19:59
-
-
Save Nek/8831799 to your computer and use it in GitHub Desktop.
working code
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
| function *listen(el, evType) { | |
| while (true) | |
| yield function(cb) { | |
| var fire = function(ev) { | |
| el.removeEventListener(evType, fire); | |
| cb(null, ev); | |
| } | |
| el.addEventListener(evType, fire); | |
| } | |
| } | |
| var gens = require('gens'); | |
| function go(gen) { | |
| gens(gen)(function(err) { if (err) throw err; }); | |
| } | |
| var jsonp = require('jsonp'); | |
| function fetch(url) { | |
| return jsonp.bind(null, url); | |
| } | |
| function render(el, items) { | |
| var items = items.map(function(item) { | |
| return '<li>' + item + '</li>'; | |
| }); | |
| el.innerHTML = '<ul>' + items.join(''); + '</ul>'; | |
| } | |
| go(function*() { | |
| var results = document.getElementById('results'); | |
| var query = document.getElementById('query'); | |
| var clicks = listen(document.getElementById('search'), 'click'); | |
| while (true) { | |
| yield clicks.next().value; // wait for 'click' event | |
| var url = 'http://en.wikipedia.org/w/api.php' + | |
| '?action=opensearch&format=json&search=' + | |
| encodeURIComponent(query.value); | |
| var data = yield fetch(url); | |
| render(results, data[1]); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment