27

I got a printerList computed property that should be re-evaluated after getPrinters() resolve, but it look like it's not.

sources are online: optbox.component.vue, vuex, optboxes.service.js

Component

<template>
    <div v-for="printer in printersList">
        <printer :printer="printer" :optbox="optbox"></printer>
    </div>
</template>
<script>
…
created() { this.getPrinters(this.optbox.id); },
    computed: {
        printersList() {
            var index = optboxesService.getIndex(this.optboxesList, this.optbox.id);
            return this.optboxesList[index].printers
        }
    },
    vuex: {
        actions: { getPrinters: actions.getPrinters,},
        getters: { optboxesList: getters.retrieveOptboxes}
    }
<script>

Actions

getPrinters({dispatch}, optboxId) {
    printers.get({optbox_id: optboxId}).then(response => {
        dispatch('setPrinters', response.data.optbox, response.data.output.channels);
    }).catch((err) => {
        console.error(err);
        logging.error(this.$t('printers.get.failed'))
    });
},

Mutations

setPrinters(optboxes, optboxId, printers) {
    var index = this.getIndex(optboxes, optboxId);
    optboxes[index] = {...optboxes[index], printers: printers }
},

Question

Why does the printerList computed property isn't re-evaluated (i.e. the v-for is empty)

Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178
  • Does the `vuex` debugger show the correct values for `Printers`? For me, it rings an alarm that mutations are passing the state around instead of handling it by themselves. –  Aug 08 '16 at 01:08
  • @HectorLorenzo if you mean the vue devtools tool,yes, but it's not commited – Édouard Lopez Aug 08 '16 at 07:20

3 Answers3

31

It is due to this line: optboxes[index] = {...optboxes[index], printers: printers }.

You are directly setting item with index, which can't be observed by Vue

Try splicing the old item from array and pushing the new one.

Community
  • 1
  • 1
bartlomieju
  • 418
  • 3
  • 4
18

You could do this:

Vue.set(optboxesList[index], 'printers', printers )
Gabriel Robert
  • 3,012
  • 2
  • 18
  • 36
  • 1
    this is what fixed it for me. I was setting the state directly from within my mutations. Once I started using Vue.set then everything became magically reactive again – vesperknight Dec 25 '17 at 00:17
1

you need force update

setPrinters(optboxes, optboxId, printers) {
    const index = this.getIndex(optboxes, optboxId);
    const newVal = {...optboxes[index], printers: printers }
    Vue.set(optboxes, index, newVal);
},