Last active
January 25, 2017 03:57
-
-
Save appsparkler/bb49f6d58d9b734d8c26a4790bd0dfe6 to your computer and use it in GitHub Desktop.
beforeMount hook in Vue.js framework
This file contains hidden or 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
export default { | |
beforeMount | |
} | |
function beforeMount(){ | |
/* | |
This is one of the most important functions for the hacker-news app | |
*/ | |
const | |
vm = this, | |
{ state, dispatch } | |
= vm.$store, | |
itemId | |
= vm.$route.params.itemId, | |
fetchItems | |
= ids => dispatch('FETCH_ITEMS', { ids }), | |
getFetchedItem | |
= () => Promise.resolve(state.items[itemId]), | |
getAllCommentsForItem | |
= item => item.kids ? fetchItems(item.kids).then(() => fetchKidComments(item.kids)) : null, | |
fetchKidComments | |
= kids => Promise.all(kids.map(kid => getAllCommentsForItem(state.items[kid]))) | |
fetchItems([ itemId ]) | |
.then(getFetchedItem) | |
.then(getAllCommentsForItem) | |
.then(() => vm.loading = false) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment