Caching requested data
See original GitHub issueHello! And thanks for creating and maintaining this great lib!
I’ve read the README and checked the issues, but couldn’t find a way to “cache” the data to avoid requesting it again. For example, my view requests the data. Then I route to another view, and later I come back. The data will be requested again.
One option would be to check in the component, before requesting the data, to see if the data is already there. But instead of each component doing the check, it would be better if the store did it. Maybe the beforeRequest
can be used for this?
And sometimes the same endpoint can return data about different elements. I try to store them in an object, like the code bellow. But then, how can I get the element “id” in the onSuccess
, so I can store it in the object?
Component
mounted () {
this.getYearInfo({ params: { year: this.$route.params.year } })
},
computed: mapState({
yearsInfo: state => state.yearInfo
}),
methods: {
...mapActions([
'getYearInfo'
])
}
Store
const year = new Vapi({
baseURL: baseUrls.year,
state: {
yearInfo: {},
}
}).get({
action: 'getYearInfo',
property: 'yearInfo',
path: ({ year }) => `/info/${year}`,
onSuccess: (state, payload, axios) => {
// How do I get the year here?
state.yearInfo[year] = payload.data
}
}).getStore()
I saw something about axios
being able to cache GET requests. Would it be a better solution for this issue?
Issue Analytics
- State:
- Created 5 years ago
- Comments:8 (5 by maintainers)
Wow! Thank you! =D
I’ve added the
{params, data}
as additional parameter in theonSuccess
andonError
functions so you can access them now. Therefore please update to v2.10.0