如何控制 amplify.request cache:persistent 选项
How to control the amplify.request cache:persistent option
当使用amplify.request时,我想使用 amplify.store 来持久化每次调用的结果。我知道我可以用这段代码做到这一点:
amplify.request.define( "ajaxExample2", "ajax", {
url: "/myApiUrl",
dataType: "json",
type: "GET",
cache: "persist"
});
但是,我希望能够将其结果添加到与 amplify.store 存储的另一个变量中,但前提是数据来自服务器而不是缓存。
例如,我将对 amplify.request("ajaxExample2"...
进行初始调用,然后我将通过 amplify.store
将结果保存到另一个变量中。然后,下次我调用 amplify.request("ajaxExample2"...
时,如果数据来自服务器,我想覆盖我的局部变量 - 如果没有,我想不理会它。
因此,我想要一种方法来知道请求是否进行服务器调用,如果调用了,则调用函数。有没有办法用放大来做到这一点?
@swatkins在调用服务之前,您可以检查存储中是否有数据(缓存),则无需调用服务,否则调用服务。
例如:
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)
});
}
相关文章:
- Ajax调用ERror Cross Origin REquest:在rails中自动完成大学列表
- https.request ignoring rejectUnauthorized
- Javascript ajax request
- node js post request.body undefined
- 带有Request.Form的JScript数组
- 在http服务单元测试中使用request-js时,chai.request不是一个函数
- 正在发送请求中包含数组的qx.io.request.Xhr请求
- Angular Cache $resource Result
- Chrome离线不会使用cache.manifest
- Angular Cross Origin Request CORS失败,但节点http.get()成功返回
- curl post request jquery equivalent
- dojo/request PUT方法dosn'不起作用
- PHP无法检索request.send
- 如何在 jquery post request 中发送api_key和api_secret参数
- webpack-request(jquery)获胜'不起作用
- Ajax-Request:将动态内容传递给模态
- 发送数组 ajax.request 原型
- 在 http 请求中添加事件侦听器 ( request.on ) 是什么意思?而笏就是它的用途
- Angular JS $http request?
- 如何控制 amplify.request cache:persistent 选项