0

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?

swatkins
  • 13,530
  • 4
  • 46
  • 78

1 Answers1

0

@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)
                });
 }
Vivek jain
  • 86
  • 3
  • This answers my question ( as I had written it ), so I've marked your answer as accepted. I should have, however, noted that I wanted to expire the cache after 1 minute. I'm looking for a way to find out if the amplify request is grabbing data from the cache or the server at various times throughout the page load - not just on initial load. – swatkins Jun 20 '12 at 15:09