Created
March 2, 2012 22:49
-
-
Save egaumer/1962044 to your computer and use it in GitHub Desktop.
Example of storing and retrieving a document by its id with the Cloud9 Javascript API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<head> | |
<title>Simple Search</title> | |
<!-- Example search template using underscore.js --> | |
<script type="text/template" id="results"> | |
<li><%= title %></li> | |
</script> | |
</head> | |
<body> | |
<div class="search"></div> | |
<script src="js/jquery-1.6.2.min.js"></script> | |
<script src="js/underscore-min.js"></script> | |
<script src="js/c9/c9api.min.js"></script> | |
<!-- change 'yourapp' to the name of your Cloud9 application --> | |
<script defer src="yourapp/js/store.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* An example of storing and retrieving a document by the id in Cloud9 */ | |
(function($) { | |
/* the document we want to index */ | |
var doc = { | |
title: "My Document", | |
date: "2012-03-02" | |
}; | |
/* our two request objects */ | |
var putRequest = {collection: "foo", type: "bar", id: "1", source: doc}; | |
var getRequest = {collection: "foo", type: "bar", id: "1"}; | |
/* a function to display results */ | |
var resultCallBack = function(document) { | |
if (document.exists) { | |
var template = _.template($("#results").html(), document._source); | |
$(".search").empty(); | |
$(".search").append(template); | |
} | |
}; | |
/* store the document */ | |
c9.index.Document(putRequest).put(function(data) { | |
if (data.ok) { | |
/* if the store was successful, fetch the doc back */ | |
c9.index.Document(getRequest).get(resultCallBack); | |
} else { | |
console.log("Error saving document"); | |
} | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment