Skip to content

Instantly share code, notes, and snippets.

@stevegraham
Created June 2, 2011 17:06
Show Gist options
  • Select an option

  • Save stevegraham/1004822 to your computer and use it in GitHub Desktop.

Select an option

Save stevegraham/1004822 to your computer and use it in GitHub Desktop.
{
"_id": "_design/example",
"_rev": "8-9dfbf79df2f15317ef69e37e0a70a704",
"validate_doc_update": "function(newDoc,oldDoc,user) {
function require(prop, message) {
message = message || prop + 'is required'
if(!newDoc[prop]) throw({'forbidden': message })
}
require('title')
require('body')
}",
"views": {
"all": {
"map": "function(doc) { emit(null, { title: doc.title, body: doc.body } ) }"
}
}
}
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
</head>
<body>
<h1>Posts</h1>
<ol id="posts"></ol>
<hr />
<form id="form">
<input type="text" name="title" value="title" /><br />
<textarea name="body">type your post</textarea><br />
<input type="submit" />
</form>
<script type='text/javascript'>
$(document).ready(function() {
$.get('http://127.0.0.1:5984/posts/_design/example/_view/all', function(data) {
data = $.parseJSON(data)
var el = $('#posts')
for(var i=0; i<data['rows'].length; i++) el.append($("<li><h2>" + data['rows'][i]['value']['title'] + "</h2><p>" + data['rows'][i]['value']['body'] + "</p></li>"))
})
})
$('#form').submit(function(event) {
event.preventDefault()
$.ajax({
url: 'http://127.0.0.1:5984/posts/',
type: 'POST',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: '{ "title":"' + $('#form input').val() + '", "body":"' + $('#form textarea').val() + '"}',
success: function(data) { $('#posts').append($("<li><h2>" + $('#form input').val() + "</h2><p>" + $('#form textarea').val() + "</p></li>")) }
})
})
</script>
</body>
</html>
@stevegraham
Copy link
Copy Markdown
Author

need to disable SOP on browser for this to work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment