Skip to content

Instantly share code, notes, and snippets.

@DreySkee
Created June 16, 2017 16:42
Show Gist options
  • Save DreySkee/1ea3631169d1c37d658ed6157140da56 to your computer and use it in GitHub Desktop.
Save DreySkee/1ea3631169d1c37d658ed6157140da56 to your computer and use it in GitHub Desktop.
8 - Wordpress API + ReactJS (Updated)
import alt from 'flux/alt/alt.js';
import DataActions from 'flux/actions/DataActions.js';
class DataStore {
constructor() {
this.data = {};
this.bindListeners({
// Listen to the getSuccess() in DataActions.js
handleSuccess: DataActions.GET_SUCCESS
});
this.exportPublicMethods({
getAll: this.getAll,
getAllPages: this.getAllPages,
getAllPosts: this.getAllPosts,
getPageBySlug: this.getPageBySlug
});
}
// Store data returned by getSuccess() in DataActions.js
handleSuccess(data) {
this.setState({ data });
}
// Returns all pages and posts
getAll() {
return this.getState().data;
}
// Returns all Pages
getAllPages() {
return this.getState().data.pages;
}
// Returns all Posts
getAllPosts() {
return this.getState().data.posts;
}
// Returns a Page by provided slug
getPageBySlug(slug){
const pages = this.getState().data.pages;
return pages[Object.keys(pages).find((page, i) => {
return pages[page].slug === slug;
})] || {};
}
}
export default alt.createStore(DataStore, 'DataStore');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment