framework7io / framework7

Full featured HTML framework for building iOS & Android apps
http://framework7.io
MIT License
18.06k stars 3.23k forks source link

Use variable for store getters #4083

Closed Simone4e closed 1 year ago

Simone4e commented 1 year ago

Is your feature request related to a problem? Please describe.

It would be very useful to have the possibility to use a variable for the "getters" function that is not necessarily initialized in the "state", since for complex systems having everything already preset becomes cumbersome.

Describe the solution you'd like The simplest solution exactly like "actions" is to add the possibility of having an additional parameter Example:

f7.store.getters.retriveUser(idUser).value;
retriveUser( { state }, idUser) {
  return state.users.find(el => el.id === idUser);
}

Describe alternatives you've considered

At the moment for each "filter" in the "getters" it is necessary to pre-set a variable in the "state" with the required value before the request and then cancel it once the page is exited or the operation is completed, this clearly increases the steps considerably. Example:

const store = createStore({
  state: {
    users: [...],    
    idUserFilter: null
  },
  getters: {
    retriveUser( {state} ) {
      return state.user.find(el => el.id === state.idUserFilter);
    }
  },
  actions: {   
    updateFilterUser({state}, idUser) {
      state.idUserFilter = idUser;
    }
  }
});
/*
 *  In some router  
 */
const idUser = 1;
f7.store.dispatch('updateFilterUser', idUser).then(() => {
    const user = f7.store.getters.retriveUser.value;
        f7.store.dispatch('updateFilterUser', null);
});
nolimits4web commented 1 year ago

Ideally you shouldn't use getters in that way, in Router Components there is a $store helper, and once you defined the getter, it will be auto updated, and you only need to modify the state with actions like here https://framework7.io/docs/store#usage-with-router-components

Simone4e commented 1 year ago

Ideally you shouldn't use getters in that way, in Router Components there is a $store helper, and once you defined the getter, it will be auto updated, and you only need to modify the state with actions like here

Yes, it's what i did for now, i update state then use getters.