Pregunta

When using amplify.request, I'd like to use amplify.store to persist the results from each call. I know I can do this with this code:

amplify.request.define( "ajaxExample2", "ajax", {
  url: "/myApiUrl",
  dataType: "json",
  type: "GET",
  cache: "persist"
});

However, I'd like to be able to add the results of this to another variable stored with amplify.store but only if the data comes from the server and not from the cache.

For example, I'll make an initial call to amplify.request("ajaxExample2"..., then I'll save the results to another variable via amplify.store. Then, the next time I make a call to amplify.request("ajaxExample2"..., if the data comes from the server, I want to overwrite my local variable - if not, I want to leave it alone.

So, I'd like a way to know if the request makes a server call or not, and call a function if it does. Is there a way to do this with amplify?

¿Fue útil?

Solución

@swatkins before calling service you can check whether store has data or not if data is available in the store (Cache) then no need to call service otherwise call service.

for ex:

    var cachedData = amplify.store("anyKey");
    if (typeof cachedData === 'undefined' || cachedData.length == 0) {

        amplify.request.define("ajaxExample2", "ajax", {
            url: "/myApiUrl",
            dataType: "json",
            type: "GET",
            cache: "persist"
        });

amplify.request("ajaxExample2", 
                 function( data ) {     
                   cachedData = data.d;
                   amplify.store("anyKey", data)
                });
 }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top