Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Limitation of mutations when passed 'state' rather than store #51

Closed
tremendus opened this issue Feb 11, 2016 · 1 comment
Closed

Limitation of mutations when passed 'state' rather than store #51

tremendus opened this issue Feb 11, 2016 · 1 comment

Comments

@tremendus
Copy link

How is one supposed to set nested keyPaths in state mutations, given that mutations only receive state and not store and therefore by implication no $set() method?

Take for instance the following, in the examples:

[DECREMENT] (state) {
  state.count--
}

.. which is fine if you need to set or replace a root level keyPath

... but what when you need to set something nested, eg:

[UPDATE] (state, keyPath, value) {
    // keyPath = state.user.userData.presence.status
}

What's the 'vue-y' way to change the value?

Using Baobab, we could set a cursor to that point or using the $set () method we could specify the dot notation keypath ... but when we only have the state object ... ?

@tremendus
Copy link
Author

So, here is what I decided to: I decided import the store into the mutations closure and use the _vm.$set() function to apply the changes to the keyPath.

import store from './index'

// ... other mutations stuff stuff ... 

[SET] (state, keyPath, val) {
    store._vm.$set('state.' + keyPath, val)
}

I don't know if this would be a recommended practice, but it does set the data and leave it all reactive, but perhaps more elegant means might be considered such as proxying the $set() method or something.

Closing issue ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant