Last active
January 31, 2017 00:23
-
-
Save DreySkee/86835b1774a9957f09aba6bb0be35479 to your computer and use it in GitHub Desktop.
9 - Wordpress API + ReactJS
This file contains 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
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