Skip to content

Instantly share code, notes, and snippets.

@graciano
Last active May 30, 2018 18:30
Show Gist options
  • Save graciano/d55cb9a92bbcd64f98e99d9e124be312 to your computer and use it in GitHub Desktop.
Save graciano/d55cb9a92bbcd64f98e99d9e124be312 to your computer and use it in GitHub Desktop.
js util functions
import axios from 'axios'
export default async function (path, defaultResult) {
try {
const response = await axios.get(path)
return response.data
} catch (err) {
console.log(path, defaultResult, err)
}
return defaultResult || {}
}
export default function (destination, source) {
Object.entries(source).forEach(entry => {
const [key, value] = entry
destination[key] = value
})
}
<template>
<section>
<h1>{{title}}</h1>
<ul>
<li v-for="elem in list" :key="elem.id">
{{ elem.name }}
</li>
</section>
</template>
<script>
import apiLoadAsync from './apiLoadAsync'
import objEntriesCopy from './objEntriesCopy'
const pathToContent = id => process.env.CONTENT_ENDPOINT + id
const loadContent = async id => apiLoadAsync(pathToContent(id))
export default {
name: 'TheContent',
data () {
return {
title: 'Loading Content..',
list: []
}
}
async mounted () {
const content = await loadContent(this.$route.params.id)
objEntriesCopy(this, content)
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment