简体   繁体   中英

How to control the amplify.request cache:persistent option

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 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)
                });
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM