Vuex: Can you add hooks before and after mutation actions ?

Created on 11 May 2018  路  10Comments  路  Source: vuejs/vuex

What problem does this feature solve?

come true global loading plugins

What does the proposed API look like?

for example:
store.subscribeBeforeAction()
store.subscribeAfterAction()
store.subscribeBeforeMutation()
store.subscribeAfterMutation()

Most helpful comment

I think I get what you mean. I think something like that was already discussed, we'll see if we willl implement it. If we do, it won't be "tomorrow".

Until then you can write simply action enhancers on your own:

function enhanceAcion(actionFn) {

  return function (context, payload)  {
    // initialize global loading status

    actionFn.call(this, context, payload).then(() => {
      // disable gloabl loading status.
    })
  }
}

// in a store module:

const actions = {
  myAction: enhanceAction(({commit}, payload) => {
    //... your normal action. make sure to return a promise.
  })
}

IF you have questions about this, please use forum.vuejs.org or chat.vuejs.org, don't ask here - this issue is about your proposal, not my workaround.

All 10 comments

come true global loading plugins

I assume that this is because of a language barrier but I don't have antsy idea what you mean by that.

Sorry, I hope vuex is supports global loading status

see title , that is also problem

@LinusBorg

I think I get what you mean. I think something like that was already discussed, we'll see if we willl implement it. If we do, it won't be "tomorrow".

Until then you can write simply action enhancers on your own:

function enhanceAcion(actionFn) {

  return function (context, payload)  {
    // initialize global loading status

    actionFn.call(this, context, payload).then(() => {
      // disable gloabl loading status.
    })
  }
}

// in a store module:

const actions = {
  myAction: enhanceAction(({commit}, payload) => {
    //... your normal action. make sure to return a promise.
  })
}

IF you have questions about this, please use forum.vuejs.org or chat.vuejs.org, don't ask here - this issue is about your proposal, not my workaround.

Great Idea Thanks

Chinese Help Tips : 鍏ㄥ眬loading鍖呰

@LinusBorg this hack doesn't work if helper mapActions is used. Then payload is undefined.

Wouldn't see why as it's passed through transparently.

If you can come up with a reproduction, post it to forum.vuejs.org and I'll have a look.

Dead issues are not the place.

Wouldn't plugins allow to do exactly that? From the example here:

store.subscribe(mutation => {
   if (mutation.type === 'UPDATE_DATA') {
     socket.emit('update', mutation.payload)
   }
 })
Was this page helpful?
0 / 5 - 0 ratings