Skip to content

Instantly share code, notes, and snippets.

@lucamug
Created February 5, 2019 21:29
Show Gist options
  • Save lucamug/fc29f4d7affd039905f1174d3728b0ef to your computer and use it in GitHub Desktop.
Save lucamug/fc29f4d7affd039905f1174d3728b0ef to your computer and use it in GitHub Desktop.
<div id="app">
<div v-for="product in products">
<input type="number" v-model="product.quantity">
<button @click="product.quantity += 1">
Add 1
</button>
{{ product.name }}
</div>
<p>Total Inventory: {{ totalProducts }}</p>
<p>Quantities: {{products.map(function(p){return p.quantity})}}</p>
</div>
<script src="https://unpkg.com/vue"></script>
<script>
const app = new Vue({
el: '#app',
data: {
products: []
},
computed: {
totalProducts() {
return this.products.reduce((sum, product) => {
return sum + product.quantity
}, 0)
}
},
created: function () {
fetch('https://api.myjson.com/bins/74l63')
.then(response => response.json())
.then(json => {
this.products = json.products
})
}
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment