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

Modules states can't be overwritten in their entirety #82

Closed
steffanlloyd opened this issue Mar 6, 2016 · 4 comments
Closed

Modules states can't be overwritten in their entirety #82

steffanlloyd opened this issue Mar 6, 2016 · 4 comments

Comments

@steffanlloyd
Copy link

A couple things at once here, sorry. Hopefully I'm posting this in the correct location, apologies if not – I'm not a seasoned github user.

This is best shown by example. The following code, which is a module in my vuex store, doesn't work as expected (i.e. the changes aren't tracked by vue).

Note: this module is called "comments", the state variable in the mutation is named comments just because that's exactly what it is.

// "comments" module

const state = {};

const mutations = {
    /**
    * Stores the data from an ajax request into the state of comments
    * @param comments [the state variable]
    * @param data [the ajax data from server, an object containing a 'comments' property, which it itself is an object
    **/
    COMMENT_SETDATA (comments, data){
        comments = data.comments;
    },
};

export default {
    state,
    mutations
};

If I were to change this to comments.all = data.comments it would work, but just assigning this top level variable itself doesn't.

[Updated]: Clarified the situation a bit better.

@yyx990803
Copy link
Member

State is an argument, which is just a local variable inside the function, assigning it to something else obviously won't work, that's how javascript works. The gist is that instead of replacing it like that, you should always treat the state as an irreplaceable root and hold actual state as properties under it.

@steffanlloyd
Copy link
Author

Well explained, thanks – sorry to bring up a non-issue.

@NoahCardoza
Copy link

I'm wondering what the best practice would be when trying to achieve this?
I wrote this

  [SET_FILE_SYSTEM]: (state, details) => {
    emptyObjects(state, details)
    mergeObjects(state, details)
  }

But I have a felling this isn't exatly a best practice...

@BenRichter
Copy link

BenRichter commented Sep 19, 2018

Better late than sorry... just as a reference. Overwrite whole store with response data:

SET_COMMENTS (state, response) {
  Object.assign(state, response)
}

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

4 participants