a Javascript starter template for creating object actions using the Stamplay Rest API
NOTE: Users must be logged in to use the rate/vote/comment function!
CLONING: When cloning this repo, you must initialize your app first to make it work.
- Initialize the front-end of your app with Stamplay
- Go to your command line and enter **stamplay init** - When prompted, enter your **appID** & **API Key**
- Establish object to be commented on
- Go to your Stamplay editor and go to the Data section. Then go to Objects
- Establish which object in the collection your going to comment on by getting it's object Id.
- Comment
function createObjectComment(){
var comment = document.getElementById('comment').value;
var data = JSON.stringify({
"text": comment
});
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
console.log(this.responseText);
}
});
xhr.open("PUT", "https://[yourappid].stamplayapp.com/api/cobject/v1/:cobjectId/:id/comment");
xhr.setRequestHeader("accept", "application/json");
xhr.setRequestHeader("content-type", "application/json");
xhr.send(data);
}
- Establish object to be rated
- Go to your Stamplay editor and go to the Data section. Then go to Objects
- Establish which object in the collection your going to rate by getting it's object Id.
- Rate
function rateFive(){
var data = JSON.stringify({
"rate": 5
});
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
console.log(this.responseText);
}
});
xhr.open("PUT", "https://[yourAppId].stamplayapp.com/api/cobject/v1/cobjectId/:id/rate");
xhr.setRequestHeader("accept", "application/json");
xhr.setRequestHeader("content-type", "application/json");
xhr.send(data);
}
- Establish object to be voted on
- Go to your Stamplay editor and go to the Data section. Then go to Objects
- Establish which object in the collection your going to upvote/downvote by getting it's object Id.
- Upvote
function upvote(){
var data = JSON.stringify({
"type": "upvote"
});
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
console.log(this.responseText);
}
});
xhr.open("PUT", "https://[yourAppId].stamplayapp.com/api/cobject/v1/:cobjectId/:id/vote");
xhr.setRequestHeader("accept", "application/json");
xhr.setRequestHeader("content-type", "application/json");
xhr.send(data);
}
- Downvote
function downvote(){
var data = JSON.stringify({
"type": "downvote"
});
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === this.DONE) {
console.log(this.responseText);
}
});
xhr.open("PUT", "https://[yourAppId].stamplayapp.com/api/cobject/v1/:cobjectId/:id/vote");
xhr.setRequestHeader("accept", "application/json");
xhr.setRequestHeader("content-type", "application/json");
xhr.send(data);
}
- Run app
- To run your app, you'll need to install the Stamplay Command Line Tool. If you have already installed it, ignore this step. Otherwise, enter this command in your command line:
$ npm install -g stamplay-cli