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

Mutation requires object reference to change #100

Open
timvahlbrock opened this issue May 14, 2021 · 0 comments
Open

Mutation requires object reference to change #100

timvahlbrock opened this issue May 14, 2021 · 0 comments

Comments

@timvahlbrock
Copy link

If a store has a property that is an object, a mutation only triggers updates if the reference of the property is changed. See the following example.

Using the following store.

interface FilterType {
  titleContains: string,
  organizerId: number
}

export class ContestsStore extends VuexModule {
    private filter = {} as FilterType;

    public get filterAsArray (): Parameters<ContestControllerApi['findAll']> {
        return [
            this.filter.titleContains,
            this.filter.organizerId
        ];
    }

    @mutation
    addFilter (modification: FilterType): void {
        for (const [key, value] of Object.entries(modification)) {
            this.filter[key as keyof FilterType] = value;
        }
    }
}

A watcher is created for filterAsArray, but the watcher does not update when addFilter is called, because the reference to the filter stays the same. addFilter does only cause an update when it's implemented like this.

    @mutation
    addFilter (modification: FilterType): void {
        const newFilter = Object.assign({}, this.filter);
        for (const [key, value] of Object.entries(modification)) {
            newFilter[key as keyof FilterType] = value;
        }
        this.filter = newFilter;
    }

I'm not sure whether this is a bug or intended. In case of the later one, if haven't found any documentation for this case anywhere and would like if there was a possibility to cause an update anyway.

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