I think I have a misunderstanding of the work with vue + vuex.
Given the following scenario.
I have a vuex state which contains a filter for a list. Each filter item in the could be marked as selected. BUT - the vuex state should only modified through a modifier when an apply button was clicked, so that other components could react to the change. For example the list would reload data depending on the selected filters. When the state updates, the component should be updated.
I have created a pen
https://codepen.io/anon/pen/OzEJWP?editors=1010
https://jsfiddle.net/rxqu7ame/
Vue.component('filter-item', {
template: ` <div>
<!--{{item.color}} ({{item.selected}}) <button @click="item.selected = !item.selected">toggle</button>-->
{{item.color}} ({{isSelected}}) <button @click="isSelected = !isSelected">toggle</button>
</div>`,
data: function () {
return {
isSelected: this.item.selected
}
},
computed: {
/*isSelected : function(){
return this.item.selected;
}*/
},
props: ['item']
});
I ran into different problems.
- When I toggle the ‚selected‘ property directly within the „filter-item" template, the vuex state would also updated.
- So I tried to initialize a data property with the state. Now only the data variable ‚isSelected‘ would be updated. When I press the ‚apply‘ button, the vuex state would be updated (later I would use a mutation) . So far, so good. But now, the ‚isSelected‘ property would not automatically updated when the state changed.
- If I use a computed property the „isSelected“ could not be change in the click event.
What is the right way to archieve the given scenario?
Many Thanks!