Answer the question
In order to leave comments, you need to log in
State as objects?
There is a state list, everything is ok while it is one. Then bam, the second list appeared on the page, the 3rd, and so on. Naturally, since the lists are already different, it is necessary for them to initialize other states for them, but that's the trouble, more than 80% of the code is absolutely identical.
For example, the page has a list of comments and a list of photos for a product, or another list of TOP comments, recommendations, menu tabs, etc.
For example, in the state it will be everywhere
state {
list: [],
}
actions: {
additemToList({state}, {item});
},
getters: {
getListItemByKey({state}, {itemKey});
}
Store({
modules:
listA,
listB,
});
class ListA extends AbstractList{}
class ListB extends AbstractList{}
abstract class AbstractList{
object list = {};
getList(){
return this.list;
}
addItemToList(item){
this.list.push(item);
}
}
const abstractListActions = {
addItemToList({commit}, {item}){
commit('addItem', item); // mutator trigger
}
};
export default abstractListActions;
const listAActions = {
removeItemByUrl({commit}, {url}) { // custom list method
commit('removeByUrl', url); // mutator trigger
}
};
export default listAActions.concat(abstractListActions);
Answer the question
In order to leave comments, you need to log in
If I'm not mistaken, then in this example, a similar task is solved in vue.js: https://github.com/vuejs/vue-hackernews-2.0
Code snippet:
export function createStore () {
return new Vuex.Store({
state: {
activeType: null,
itemsPerPage: 20,
items: {/* [id: number]: Item */},
users: {/* [id: string]: User */},
lists: {
top: [/* number */],
new: [],
show: [],
ask: [],
job: []
}
},
actions,
mutations,
getters
})
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question