Skip to content

Instantly share code, notes, and snippets.

@DreySkee
Last active January 31, 2017 00:23
Show Gist options
  • Save DreySkee/86835b1774a9957f09aba6bb0be35479 to your computer and use it in GitHub Desktop.
Save DreySkee/86835b1774a9957f09aba6bb0be35479 to your computer and use it in GitHub Desktop.
9 - Wordpress API + ReactJS
import alt from './../alt/alt.js';
import DataActions from './../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,
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().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