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

How to specify namespace for mapFields through component props #51

Closed
max-frai opened this issue Feb 22, 2019 · 3 comments
Closed

How to specify namespace for mapFields through component props #51

max-frai opened this issue Feb 22, 2019 · 3 comments

Comments

@max-frai
Copy link

I want to pass namespace from vuex module with props but it seems to be impossible.

<my-component namespace="ns1" />

// my-component code
export default {
  props: ["namespace"],
  computed: {
    ...mapFields(??this.namespace??, ["attr1", "attr2"])
  }
}

Of course, there is no way to use this in such way so we don't have access to props. How can I specify namespace as prop in such case?

@max-frai max-frai changed the title How to specify namespace for mapGetters through component props How to specify namespace for mapFields through component props Feb 22, 2019
@max-frai
Copy link
Author

max-frai commented Feb 22, 2019

I found a dirty workaround. I rewrote method mapFields and calculate getterType in runtime:

    ...
    const field = {
      get() {
        var getterType = `${this.namespace}/getField`;
        return this.$store.getters[getterType](path);
      },
      set(value) {
        var getterType = `${this.namespace}/updateField`;
        this.$store.commit(mutationType, { path, value });
      }
    };
    ...

Of course, this required namespace to be declared. I understand this is dirt and not for general usage, but maybe it's possible to rewrite mapFields with such functionality?

@max-frai
Copy link
Author

If it's okay, I can write pull request modifying mapFields with ability to specify inner prop name that we should use in runtime for namespace calculating.

@maoberlehner
Copy link
Owner

Hey @max-frai! Thank you for your input. If I understand it correctly, this is a duplicate of #24, please use the existing issue for further discussion (if it is no duplicate, please let me know and I'll reopen this issue).

I'll consider accepting a pull request if it can be done in a backwards compatible manner and I'm happy with the resulting API.

Thank you for contributing!

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

No branches or pull requests

2 participants