Skip to content

Instantly share code, notes, and snippets.

@boycaught
Created January 24, 2013 15:23
Show Gist options
  • Save boycaught/4622976 to your computer and use it in GitHub Desktop.
Save boycaught/4622976 to your computer and use it in GitHub Desktop.
CouchDB snippet:
// Nicked from: http://feedproxy.google.com/~r/cloudant/~3/3sqjw_b2Ekg/make-it-easy
// Dependencies: JQuery and sag.js (http://www.saggingcouch.com/)
$(function() {
// since I want to work locally (but use Cloudant data), I've set up a reverse proxy
// more on reverse proxy: http://wiki.apache.org/couchdb/Apache_As_a_Reverse_Proxy
// use sag.js to connect
var user_db = 'my-Cloudant-database-name';
var couch = sag.server('my-server'); // localhost
couch.setDatabase(user_db);
// capture the search form submission
$('form.search').submit(function(e){
// kill default action
e.preventDefault();
// get the value of the search input
var query = $('#last').val();
// get data via sag.js
couch.get({
// query my Cloudant search index (see above) and account for wildcards (partial string searches)
url: '/_design/people/_search/searchByName?include_docs=true&q=' + query + '*',
callback: function(resp, succ) {
// vars for iterating over the results
var rows = '', i;
// create the rows for my table
for(i in resp.body.rows) {
rows += '<tr><td>';
rows += '<a href="#" class="view-detail data-cloudant-id="' + resp.body.rows[i].id + '">';
rows += resp.body.rows[i].doc.first + ' ' + resp.body.rows[i].doc.last;
rows += '</a></td></tr>';
}
// drop the rows into a table and append it to the results container in my interface
$('#results').html('<table id="people" class="table table-striped"><tbody>' + rows + ' </tbody></table>');
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment