Comments (8)
from vuex-rest-api.
Hi Andrés,
thanks for your compliments!
Caching
I think the easiest way to provide caching would be to make axios do it for you. https://github.com/RasCarlito/axios-cache-adapter https://github.com/kuitos/axios-extensions#cacheadapterenhancer seems promising. Just create an axios instance with this adapter and pass it to the Vapi constructor (see docs).
Getting the year
value in the onSuccess
fn
At the moment parameters (like year
) aren't easy accessible in the onSuccess
and onError
fn. I'm sorry. :(
from vuex-rest-api.
Here is an ugly way how you could get the params in the onSuccess
fn at the moment:
You could create a new property in the store and save it in the beforeRequest. Then you could call it in the onSuccess fn. It's not pretty but it works.
Example (didn't test it)
{
beforeRequest: (state, { params, data }) => {
state.year = params.year
},
onSuccess: (state, payload, axios) => {
const year = state.year
state.yearInfo[year] = payload.data
}
}
from vuex-rest-api.
Thanks for the quick replies!
Is it possible to avoid the request from happening from inside beforeRequest
? So it can check if the data is already in the store and return it, instead of doing the request.
And about the way of passing the year
to the onSuccess
using another variable like that, wouldn't it be possible that multiple requests, with delayed or out of order responses, mess up with this method?
Going back to the other alternative... I took a look at both axios caching libs. But I'm a bit worried about how to invalidate some cached items when the user modifies server data.
For example, we loaded a list of posts from /:category/posts
, and then the user sends a new post. The cached post list must be invalidated.
- Maybe storing at some place that URL so the next request to it forces an update. But it seems awkward to store URLs for invalidation like this. Data seems easier to invalidate than URLs.
- Maybe doing the non-cached request for the post list just after the user sent a new post. But sometimes the post list isn't needed right now. And if many endpoints depend on the invalidated data, that can mean many unneeded requests.
from vuex-rest-api.
Is it possible to avoid the request from happening from inside beforeRequest? So it can check if the data is already in the store and return it, instead of doing the request.
This is not possible at the moment. If you use https://github.com/kuitos/axios-extensions#cacheadapterenhancer this shouldn't be a problem because the request will only be sent to the server if the cache expires or the response isn't cached yet.
And about the way of passing the year to the onSuccess using another variable like that, wouldn't it be possible that multiple requests, with delayed or out of order responses, mess up with this method?
Yep, race condition. :(
Caching is something I didn't think about during the development of vuex-rest-api. Currently I'm developing version 3 of vuex-rest-api. I'll consider your issues and try to solve it with a new API. Unfortunately I don't think I will change this in the current version. I'm sorry.
from vuex-rest-api.
I feel I would have a better control over the cache doing it at vuex-rest-api level. But since it lacks some features needed for it, I guess I'll try axios-extensions, forcing data reload just after data modification.
Unfortunately I don't think I will change this in the current version. I'm sorry.
I understand, no problem. And thanks for considering it for v3. Feel free to close this issue if you feel so.
Thanks again for all the help!
from vuex-rest-api.
I feel I would have a better control over the cache doing it at vuex-rest-api level.
I think so, too. :)
I understand, no problem. And thanks for considering it for v3. Feel free to close this issue if you feel so.
Thanks again for all the help!
Anytime! Little note: I don't know when I'll release v3. Approximately in 2-3 months without guarantee!
Feel free to open a new issue if you have any questions.
from vuex-rest-api.
I've added the {params, data}
as additional parameter in the onSuccess
and onError
functions so you can access them now. Therefore please update to v2.10.0
from vuex-rest-api.
Related Issues (20)
- What does pending really mean? Not sure if this is intended behavior. HOT 2
- V3 - Checklist HOT 2
- Handling rejected promises HOT 3
- Update axios peer dependency HOT 6
- Can't install HOT 5
- Not possible to create a pr HOT 3
- [QUESTION] - Pre API Call Transformation HOT 3
- How to use vuex-rest-api with nuxt.js auth-module HOT 15
- Using webpack: vuex_rest_api__WEBPACK_IMPORTED_MODULE_0___default.a is not a constructor HOT 4
- Correct way to change state HOT 9
- provide action payload in onSuccess HOT 4
- Add support of mapping response object to typed model objects HOT 1
- Section "calling-the-actions" disapeared from the docs HOT 2
- Not an issue tip for CORS. [Tips] HOT 1
- Callback when rest call completes HOT 2
- Support for Fetch API HOT 4
- Add ability to add "namespaced" option for "getStore" HOT 4
- How to get rid of error&pending in setting a custom state? HOT 7
- Update peer dependency of axios HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from vuex-rest-api.