Skip to content

Instantly share code, notes, and snippets.

@appsparkler
Last active January 25, 2017 03:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save appsparkler/bb49f6d58d9b734d8c26a4790bd0dfe6 to your computer and use it in GitHub Desktop.
Save appsparkler/bb49f6d58d9b734d8c26a4790bd0dfe6 to your computer and use it in GitHub Desktop.
beforeMount hook in Vue.js framework
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