Skip to content

Instantly share code, notes, and snippets.

@jslatts
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jslatts/4b2b7177941cb8af6a28 to your computer and use it in GitHub Desktop.
Save jslatts/4b2b7177941cb8af6a28 to your computer and use it in GitHub Desktop.
Example RefluxJS Store
'use strict';
//External
var Reflux = require('reflux');
//Local
var CommentActions = require('./CommentActions');
var CommentStore = Reflux.createStore({
listenables: [CommentActions],
getInitialState() {
if (typeof document !== 'undefined') {
var storeElement = document.getElementById('storeUrls');
this.urls = JSON.parse(document.getElementById('storeUrls').textContent);
this.loadComments();
}
},
// PUT: api/Analysis/5?addComment=true
addComment(comment) {
var xhr = new XMLHttpRequest();
xhr.open('put', this.urls.addComment + '&addComment=' + 'true' + '&comment=' + comment, true);
xhr.setRequestHeader('Content-Type','application/json');
xhr.onload = () => {
if (xhr.readyState === 4) {
if (xhr.status === 204) {
this.loadComments();
}
else {
console.error(xhr.statusText);
}
}
};
xhr.send();
},
loadComments() {
var xhr = new XMLHttpRequest();
xhr.open('get', this.urls.fetchComments, true);
xhr.setRequestHeader('Content-Type','application/json');
xhr.onload = () => {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
var analysisCommentsResponse = JSON.parse(xhr.responseText);
this.analysisComments = analysisCommentsResponse;
this.trigger(analysisCommentsResponse);
}
else {
console.error(xhr.statusText);
}
}
};
xhr.send();
}
});
module.exports = CommentStore;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment