Element for interacting with the fetch api and queuing requests up if working offline
bower install fetch-offline-element --save
Include the element in your element/html page.
<link rel="import" href="../bower_components/fetch-element/fetch-element.html">
Add to your html
<fetch-element id="request"></fetch-element>
In you're javascript you can interact with it like so
let req = new Request('url', {method: 'POST', body: body})
this.$.request.fetch(req)
.then(function (response) {
if (response.status === 202){
///handle offline by returning body
return body
}
///otherwise returns a data stream, converting it to json
return response.json()
})
.then(function(stuff){
console.log(stuff)
})
Pass the fetch element a request object, created using the Request constructor
If you are offline, the element will add the request to the queue stored in the local cache and return you a 202 where you can then handle being offline
If you are online, the element will flush the cache in order which the were sent and then make a fetch request and return a promise, with a response object
Cavet the fetch api doesn't reject the promise if a 404 or 500 is returned from the server.
- Fork it
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :)