Skip to content
Go to file
Cannot retrieve contributors at this time
55 lines (51 sloc) 1.83 KB
<link rel="stylesheet" type="text/css" href="">
<style type="text/css">
label { padding : 0; margin-right : 15px;}
.ajaxIndicator {
background: url('loading.gif');
width: 16px;
height: 16px;
display: inline-block;
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="Bacon.js"></script>
<script type="text/javascript">
$(function() {
// Stream of search strings (queries)
var searches = $("#query").asEventStream("keyup").map(function(event) {
return $(event.srcElement).val()
// Stream of search result strings, with errors mapped to "Search fail"
var results = searches.throttle(500).switch(function(query) {
return Bacon.fromPromise($.ajax("/search/" + query))
}).mapError("Search fail")
// Show results
results.onValue(function(result) {
// Search status, where "true" indicates an ongoing search
var ongoingSearch =
// Replace results with placeholder when a search is going on
ongoingSearch.onValue(function(show) {
<h1>bacon.js AJAX example</h1>
<div id="search">
<label for="query">Search</label>
<input id="query" type="text">
<div id="result">
<label for="resultText">Results</label>
<span id="resultText"></span>
<span class="ajaxIndicator"></span>
You can’t perform that action at this time.