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

context.commit #114

Open
heiheshang opened this issue Jul 28, 2020 · 9 comments
Open

context.commit #114

heiheshang opened this issue Jul 28, 2020 · 9 comments
Assignees
Labels

Comments

@heiheshang
Copy link

how to do context.commit ? I used to call my method, what should I do now?

@maoberlehner
Copy link
Owner

I have no idea what you’re talking about.
See https://github.com/maoberlehner/vuex-map-fields/blob/master/CONTRIBUTING.md#reporting-issues for information how to write a proper issue. Thanks!

@heiheshang
Copy link
Author

if I use updateField, then which method should I call in context.commit. I have an item field, what will be the name of the method that is generated automatically?

@maoberlehner
Copy link
Owner

Still no idea what you're talking about. What is context.commit?
Please see https://github.com/maoberlehner/vuex-map-fields/blob/master/CONTRIBUTING.md#reporting-issues for information how to write a proper issue. Thanks!

Provide a reduced test case or a live example.

@heiheshang
Copy link
Author

heiheshang commented Jul 29, 2020

`const state = {
items: []
}

const getters = {
getField
}

const mutations = {
updateField
}

const actions = {
ITEMS: async (context, payload) => {
context.commit('SET_LOADING', true)
await axios.post('/api/model/amiro/post/list', {
queryid: uuid.list
})
.then(data => {
if (data.data.status === 'ok') {
//????
context.commit('items', data.data.result)
}
})
.catch(error => {
context.commit('SET_TEXTERROR', error)
context.commit('SET_LOADING', false)
})
},
`

@maoberlehner
Copy link
Owner

Create you own mutation for setting items.
In theory you can (ab)use the mutation created by vuex-map-fields but I advice against doing this.

@geoidesic
Copy link
Collaborator

@heiheshang have the comments here solved your issue?

@aberbenni
Copy link

aberbenni commented Sep 20, 2020

@maoberlehner Why do you advise against using mutations generated by vuex-map-fields inside actions?

In my case, I solved it like this:

mutations: {
   updateField,
   setItems(state, payload) {
      state.items = payload;
   },
}

but it seems to me useless and repetitive code since in practice setItems already exists, with another name, eg:

commit("updateField", { path: "items", value: data.data.result });

equals to:

commit("setItems", data.data.result);

@geoidesic
Copy link
Collaborator

geoidesic commented Oct 3, 2020

@aberbenni and @heiheshang The point of this library is that you don't manually call the mutations. Instead the mutations are written as setters on the aliased property in the vm root scope. So you simply set the value for that alias and the mutation is called automatically.

So for example:

computed:{
    ...mapFields('myVuexStoreModule', ['my.path.in.the.vuex.store.to.my.data.node'])

This will create an alias vm.node (i.e. this.node), then in order to call the updateField mutation you can just do this.node = 'someValue'. There's no need to manually call the mutation. It's not clear why you would be trying to call the updateField mutation from within a vuex action, as this library isn't designed for that... it's designed as a way of writing less boilerplate in your component computed property.

@aberbenni
Copy link

aberbenni commented Oct 5, 2020

I try to better clarify what I'm doing, my use case is about actions where I run asynchronous code by calling external APIs with axios and then saving fetched data in my Vuex store.

To do this, you need to commit mutations in an action, as reported here: https://vuex.vuejs.org/guide/actions.html
"Instead of mutating the state, actions commit mutations".

Here is my code:

callSomeApiAction = async (
  { commit, dispatch, state },
  payload
) => {
  try {
    dispatch("setLoadingState");
    
    //async code
    
    ...

    //store values fetched from api
    commit("updateField", {
      path: "items",
      value: data.data.result,
    });

    // Reset the loading state
    dispatch("resetLoadingState");
  } catch (error) {
    dispatch("setErrorState", error);
  } finally {
    ...
  }
};

So how can I commit? Should I use updateField or create an ad hoc mutation setItems?
...mapFields is not available in actions (which in my case are in an external file).

In order to write less boilerplate code I would prefer to use updateField inside actions instead of create redundant mutations like setItems.

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

No branches or pull requests

4 participants