I have a simple api set up for my App.
My question is, what would be the reccomendation as the best option for access it from a page to load the data:
fetch('http://myapi.com')
.then(response => response.json())
.then(data => {
self.$setState({
mydrugs: data
});
});
or:
app.request.json('http://myapi.com', function(data) {
self.$setState({
mydrugs: data
});
});
deejay
2
fetch is fetch.
app.request use xhr.
they both return ‘Promise’
performance in your simple case should be the same.
const share = {
fetch: (u) => fetch(u).then(r => r.json()),
request: (u) => app.request.json(u).then(r => r.data)
};
share.fetch('url').then(d => console.log(d));
share.request('url').then(d => console.log(d));
1 Like
deejay
3
in v5
const share = {
fetch: (u) => fetch(u).then(r => r.json()),
request: (u) => app.request.promise.json(u).then(r => r.data)
};
deejay
4
Framework7.use({
create() {
this.utils.extend(this,{
share: {
fetch: (u) => fetch(u).then(r => r.json()),
request: (u) => this.request.json(u).then(r => r.data),
promise: (u) => new Promise((r,j) => [new XMLHttpRequest()]
.map(i => Object.assign(i,{
responseType: 'json',
onerror: () => j(i),
onload: () => [i].filter(i =>
[
[
i.status >= 200, i.status < 300
].every(i => i), i.status === 0
].some(i => i))
.map(i => r(i.response))
.concat(j(i))
})).map(i => { i.open('get',u,true); i.send(); }))
}
});
}
});
let app = new Framework7({ /* params */ });
let url = 'https://jsonplaceholder.typicode.com/todos/1';
// all return the same result
app.share.fetch(url)
.then(d => console.log(d)).catch(e => console.log(e));
app.share.request(url)
.then(d => console.log(d)).catch(e => console.log(e));
app.share.promise(url)
.then(d => console.log(d)).catch(e => console.log(e));
2 Likes