Skip to content

Calling getter in mutation #75

@Glandos

Description

@Glandos

I am trying to call a getter with a mutation, and it seems not to work:

import { createModule, createProxy, mutation, action } from 'vuex-class-component'

import { Investigation, InvestigationId, InvestigationsState } from './types'

const VuexModule = createModule({
  namespaced: 'investigations'
})

export class Investigations extends VuexModule implements InvestigationsState {
  investigations: Investigation[] = []
  currentInvestigationIndex = -1

  get current (): Investigation | undefined {
    return this.currentInvestigationIndex >= 0 ? this.investigations[this.currentInvestigationIndex] : undefined
  }

  get id () {
    return (id: InvestigationId): Investigation | undefined => this.investigations.find(i => i.id === id)
  }

  @mutation
  changeSomething ({ investigationId, thing }: { investigationId: InvestigationId, thing: string }): void {
    // ERROR: this.id is not a function
    const investigation = investigationId ? this.id(investigationId) : this.current
    // Do stuff
  }
}

I can work around this by using a proxy within the mutation:

const me = createProxy(this.$store, Investigations)
const investigation = investigationId ? me.id(investigationId) : me.current

But I thought that this could have been done at a higher level.
Am I missing something?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions