Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Created March 14, 2010 22:13
Show Gist options
  • Save mattpodwysocki/332274 to your computer and use it in GitHub Desktop.
Save mattpodwysocki/332274 to your computer and use it in GitHub Desktop.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Dictionary Suggest</title>
<link rel="stylesheet" href="demo.css"/>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="rx.js" type="text/javascript"></script>
<script type="text/javascript">
function createSearchRequest(term) {
var url = "http://en.wikipedia.org/w/api.php?action=opensearch&search="+term+"&format=json";
return Rx.Observable.XmlHttpRequest(url)
.Select(function(result) {
var response = eval(result.responseText);
if(response.length == 2) return response[1];
else return [];
});
}
$(document).ready(function() {
$("#searchInput")
.ToObservable("keyup")
.Select(function(_) {
return $("#searchInput").val(); })
.Throttle(250)
.Select(function(term) {
return createSearchRequest(term); })
.Switch()
.Subscribe(
function(data) {
$("#results").empty();
$.each(data, function(index, value) {
$("#results").append("<li>"+value+"</li>");
});
},
function(error) {
$("#error").html(error);
});
});
</script>
</head>
<body>
<input id="searchInput" size="100" type="text" value="">
<ul id="results" />
<p id="error" />
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment