Created
June 16, 2017 16:42
-
-
Save DreySkee/1ea3631169d1c37d658ed6157140da56 to your computer and use it in GitHub Desktop.
8 - Wordpress API + ReactJS (Updated)
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 '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