dimanche 16 janvier 2022

Vuex: single load of several datasets

In my Vue project, there are some references data, changing very rarely (payment methods, predefined service types etc.). I can load them conditionally

    async loadPaymentTypes({state, commit}) {
        if (state.payment_types.length === 0 ) {
            const data = await axios.get('/api/v1/payment-types');
            commit(setPaymentTypes, data.data);
        }
    },

but I hate to copypaste that code (and you see, it's a prototype, I have to add error handling to all actions).

How can I make some good factory like

async loadPaymentTypes({state,commit}) {
    check_load(state.payment_types, '/api/v1/payment-types', 'setPaymentTypes')
}

?

Aucun commentaire:

Enregistrer un commentaire