GithubHelp home page GithubHelp logo

Comments (12)

Koc avatar Koc commented on May 22, 2024 2

Is it possible get rid of strings when calling mutation inside action this.context.commit('addWheel', wheels) and call class method like this.mutations.addWheel(wheels)?

from vuex-module-decorators.

talkid134 avatar talkid134 commented on May 22, 2024

Have you implemented this yet?

from vuex-module-decorators.

championswimmer avatar championswimmer commented on May 22, 2024

No, still figuring out what would be the best way to handle this.

from vuex-module-decorators.

talkid134 avatar talkid134 commented on May 22, 2024

Just replace field with getter/setter on @mutable annotation(decorator)?

from vuex-module-decorators.

talkid134 avatar talkid134 commented on May 22, 2024

For example

@Mutable 
data:any;

from vuex-module-decorators.

championswimmer avatar championswimmer commented on May 22, 2024

Hmmm that sounds pretty good!

from vuex-module-decorators.

FlorianWendelborn avatar FlorianWendelborn commented on May 22, 2024

@championswimmer it’d be less powerful than supporting something like this though:

class Example {
    set INITIALIZE({ id, firstName, lastName }: User) {
        this.id = id
        this.firstName = firstName
        this.lastName = lastName
    }
}

Another advantage would be that this is a lot closer to the normal vuex behavior.

from vuex-module-decorators.

barakbd-bluevine avatar barakbd-bluevine commented on May 22, 2024

Can you please give a full example of getter/setter with @mutable decoration?
Can it be used in current released?

from vuex-module-decorators.

douglasg14b avatar douglasg14b commented on May 22, 2024

@Koc That would be great, right now refactoring large stores is more tedious than it needs to be with the strings....

from vuex-module-decorators.

garyo avatar garyo commented on May 22, 2024

I would love simple @Mutable like @talkid134 suggests. I have lots of simple data members in the state -- right now each one needs a mutation to set its value. (Well I suppose I could have one uber-mutation that took the string name of the prop to update... but that would be ugly.)

from vuex-module-decorators.

pumpkinlink avatar pumpkinlink commented on May 22, 2024

Is it possible get rid of strings when calling mutation inside action this.context.commit('addWheel', wheels) and call class method like this.mutations.addWheel(wheels)?

@Koc @douglasg14b I "get rid" of strings by adapting the Vuex mutation name convention

const rawError = true
const setPage = 'setPage'

@Module
export default class ActivitiesModule extends VuexModule {
  page: Page<OperationDTO> = {
    totalElements: 0,
    content: []
  }

  @Mutation [setPage] (value: ActivitiesModule['page']) { this.page = value }

  @Mutation cleanPage ()  { this.page = { totalElements: 0, content: [] }

  @Action({ rawError })
  async pullPage (/*...*/) {
      const response = await operationService
        .getByFinancialAccount_IdAndOccurrenceDateBetween(/*...*/)

      // use this:
      this.context.commit(setPage, response.data)
      // instead of this:
      this.context.commit('setPage', response.data)
  }
}

The mutation function will stop appearing in vscode's autocomplete when using getModule, because it's a computed property name, but parameter hints and type-checking still work.

Update: you can also ditch the constant and use the class prototype instead:
Update 2: This doesn't work on production builds. ↓↓↓ For some odd reason, all .name props inside mutatation method prototypes return the string "value" when it's built with Vue CLI 3 npm run build.

const rawError = true

@Module
export default class ActivitiesModule extends VuexModule {
  page: Page<OperationDTO> = {
    totalElements: 0,
    content: []
  }

  @Mutation setPage (value: ActivitiesModule['page']) { this.page = value }

  @Mutation cleanPage ()  { this.page = { totalElements: 0, content: [] }

  @Action({ rawError })
  async pullPage (/*...*/) {
      const response = await operationService
        .getByFinancialAccount_IdAndOccurrenceDateBetween(/*...*/)

      // DONT DO THIS ↓, IT WILL LIKELY BREAK IN PRODUCTION
      this.context.commit(ActivitiesModule.prototype.setPage.name, response.data)
  }
}

from vuex-module-decorators.

Glandos avatar Glandos commented on May 22, 2024

As a "workaround", I use vuex-module-decorators along with Pathify:

import { VuexModule, Module } from 'vuex-module-decorators'
import { make } from 'vuex-pathify'

@Module
class Preferences extends VuexModule {
  grid = {
    editable: false,
    textOnHover: false,
    textHoverTimeout: 1000,
  }
}

Preferences.mutations = {
  ...make.mutations(Preferences.state),
  ...Preferences.mutations
}

export default Preferences

And in my component, I use get(), set() and sync() from pathify.

from vuex-module-decorators.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.