Closed lixiasandy closed 5 years ago
Parameters just pass to sync methods directly. Nothing special.
Thank you very much for your reply. My parameters pass pages, and the data returned is different for different pages. For example, the first page returns {data:{name:'zs'} and I hope the second page returns {data:{name:'ls'}, but in sync it returns the cached information {data:{name:'zs'} of the first page.
Post your code here
//storage.js
save(key, obj) {
storage.save({
key: key,
data: obj,
expires: defaultExpires
})
},
load(key,extraFetchOptions, callBack) {
storage.load({
key: key,
autoSync: true,
syncInBackground: true,
syncParams: {
extraFetchOptions: extraFetchOptions,
someFlag: true,
}
}).then(ret => {
callBack && callBack(ret)
return ret
}).catch(err => {
console.warn(err.message);
})
}
//sync.js
async tuimusic(params) {
let {resolve, reject, syncParams:{extraFetchOptions, someFlag}} = params;
await fetch(TUIMUSIC, {
method: 'POST',
headers: {'Accept': 'application/json','Content-Type': 'application/json'},
...extraFetchOptions,
}).then(response => {
return response.json();
}).then(json => {
if (json && json.data) {
storage.save('tuimusic',json);
resolve && resolve(json)
}else{
reject && reject(new Error('data parse error'));
}
}).catch(err => {
console.warn(err);
reject && reject(err)
});
}
//index.js
let { page, data } = this.state
const params = {page:page};
storage.load('tuimusic', params, (json) => {
if (json == undefined) {
storage.load('tuimusic', params, (json) => {
this.setState({
data: data.concat(json.data.music)
})
});
}else {
this.setState({
data: data.concat(json.data.music)
})
}
});
What version are you using now? The latest version does not have resolve and reject. From you snippet the parameter depends on how you invoke your load method.
// load storage .load({ key: 'tuimusic',
}) .then(ret => { // found data go to then()
}) .catch(err => { // any exception including data not found // goes to catch() console.warn(err.message); switch (err.name) { case 'NotFoundError': // TODO; break; case 'ExpiredError': // TODO break; } });
//sync.js async tuimusic(params) {
},