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

feat: Enhance namespace helper with generic powered StateBindingHelper #54

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

hikerpig
Copy link

This PR adds some generic types to enhance StateTransformer and namespace for better type inference.

@tarik02
Copy link

tarik02 commented May 21, 2020

I think that you should implement the same for mapActions and mapMutations too. As you can read here https://vuex.vuejs.org/api/#mapactions, if you pass function instead of action/mutation name as object's value, then function will be called with dispatch/commit for actions/mutations, and rest of args.

Example:

methods: {
    ...mapActions({
        login(dispatch, username, password) {
            dispatch('login', { username, password })
        }
    })
}

@tarik02
Copy link

tarik02 commented May 21, 2020

This can be implemented in easier way:

diff --git a/src/bindings.ts b/src/bindings.ts
index f0c9231..f665791 100644
--- a/src/bindings.ts
+++ b/src/bindings.ts
@@ -93,7 +93,13 @@ function createBindingHelper (
         componentOptions[bindTo] = {}
       }
 
-      const mapObject = { [key]: map }
+      let mapObject: any
+      if (componentOptions.methods && componentOptions.methods[key]) {
+        mapObject = { [key]: componentOptions.methods[key] }
+        delete componentOptions.methods[key]
+      } else {
+        mapObject = { [key]: map }
+      }
 
       componentOptions[bindTo]![key] = namespace !== undefined
         ? mapFn(namespace, mapObject)[key]

After this, you can use it like this:

@Action
login(dispatch, username, password) {
    dispatch('login', { username, password })
}

The solution you used for state can't be normally used for actions and mutations because it is usually needed to use this in action/mutation "enhancer".

But my solution has a disadvantage: it can't be normally typed.

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

Successfully merging this pull request may close these issues.

2 participants