-
Notifications
You must be signed in to change notification settings - Fork 85
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
Computed properties still empty, when state is changed #41
Comments
Hey @meliborn, without seeing any code, there is nothing I can do for you ;) Please provide a reduced test case (you can use https://codesandbox.io/ for example) or at least copy and paste the relevant code (the |
|
Please try to set default values for the properties you want to access and let me know if it worked, thx. // initial state
import { getField, updateField } from "vuex-map-fields";
import { LOAD_COMPANY } from "@/store/action-types";
import { SET_COMPANY } from "@/store/mutation-types";
import axios from "axios";
export default {
namespaced: true,
state: {
current: {
name: '',
description: '',
company_type: '',
}
},
getters: {
getField
},
mutations: {
updateField,
[SET_COMPANY](state, company){
state.current = company;
}
},
actions: {
[LOAD_COMPANY](state) {
axios.get("/company").then(company => {
state.commit(SET_COMPANY, company.data);
});
}
}
}; |
I faced with the same issue. Setting default values fixes that but sometimes it needs to set values dynamically, using data from API. Another option is to initialize I would prefer that the library itself would check the existence of property in if (!state.hasOwnProperty(field.path)) {
Vue.set(state, field.path, field.value)
} else {
state[field.path] = field.value
} I can do that in my own What about considering adding some flag for |
It could look like this: function updateField(state, { path, value }, makeReactive = false) {
path.split(/[.[\]]+/).reduce((prev, key, index, array) => {
if (array.length === index + 1) {
if (makeReactive && !prev.hasOwnProperty(key)) {
Vue.set(prev, key, value)
} else {
prev[key] = value
}
}
return prev[key]
}, state)
} I used it instead of librarian one and it works for plain and object properties. Haven't checked with arrays yet. |
This could be an interesting addition to the code. I'd take a look at a pull request if somebody invests their time into that. 👍 |
Is it possible to add this case to the readme for now ? I actually struggled to understand where the issue was and I faced the exact same case. If I have time next week, I'll try to open a PR with a solution. |
Yes, this would be a great feature. I have many forms that are 100+ variables, really don't want to have to add defaults for every single one (and whether some fields are even shown depends on values of other variables, so you really don't have a concept of "defaults" in this case). Seems like an easy fix... though I suppose we could just wait for vue 2.6+ as I hear the requirement of forcing defaults will be removed... |
Eagerly looking forward to this feature! |
Not sure it's a vuex-map-fields problem but...
I have a simple form and submit button. I do axios call to send data to the server. The problem is, when I type something into form input, property still empty, when store is up to date.
I'm using chrome plugin to debug values:
Here is a store:
So when I submit the form it always clear filled values. What's wrong?
The text was updated successfully, but these errors were encountered: