Thursday, July 7, 2011

CouchDB with Jsonp

Download jquery.jsonp.js and ejs.js.

Templates "list.ejs"

 <ul>  
<% $.each(list, function(i, element) {%>
<li><%=element%></li>
<% }); %>
</ul>


test.html

 <!DOCTYPE html>  
<html>
<head>
<title>CouchDB Demo</title>
<%= stylesheet_link_tag 'jquery-ui', 'develop_demo' %>
<%= javascript_include_tag :defaults, 'jquery.couch', 'jquery.jsonp-2.1.4.min', 'ejs' %>
<%= csrf_meta_tag %>
<script type="text/javascript" charset="utf-8">
$(document).ready(function() {
var url_all_dbs = "http://127.0.0.1:5984/_all_dbs";
function all_dbs() {
$.jsonp({
url: url_all_dbs + "?callback=?",
success: function(data) {
render_all_dbs(data);
}
});
}
function render_all_dbs(data) {
var template = new EJS({url: '/templates/list.ejs'})
var div_html = template.render({list: data});
$("#couch_all_dbs").html(div_html);
};
all_dbs();
});
</script>
</head>
<body>
<h1>Development Test</h1>
<div id="content">
Content...
<h1>all_dbs</h1>
<div id="couch_all_dbs"></div>
</div>
</body>
</html>

No comments: