@moos/backbone-fetch
v1.0.2
Published
Backbone Models without jQuery.ajax
Downloads
3
Readme
backbone-fetch
Backbone Model CRUD operations
using ES6 fetch API. Can be used without jQuery
or jQuery.ajax
.
Install
npm i @moos/backbone-fetch
Or get it from jsDelivr CDN...
Latest:
- https://cdn.jsdelivr.net/gh/moos/backbone-fetch/backbone-fetch.js
- https://cdn.jsdelivr.net/gh/moos/backbone-fetch/backbone-fetch.min.js
Versioned (recommemded in production):
- https://cdn.jsdelivr.net/gh/moos/[email protected]/backbone-fetch.js
- https://cdn.jsdelivr.net/gh/moos/[email protected]/backbone-fetch.min.js
Usage
require('backbone');
Backbone.ajax = require('@moos/backbone-fetch'); // or use <scrip>
let model = new Backbone.Model({foo: 1});
model.save()
.then(response => {...})
.catch(error => {...});
Similar for other Backbone CRUD operations: fetch()
& destroy()
.
Note: it returns an ES6 Promise. Use polyfill for older browsers.
To pass body and headers:
model.save(null, {
data: {...} // JSON data to pass in HTTP body
headers: {
'Accept': 'application/json'
}
})
.then(response => {...})
.catch(error => {...});
If AbortController is supported (sorry IE or use a polyfill), the returned promise will have an abort()
method:
var promise = model.fetch();
// a little later...
promise.abort();
The success of abort depends on the state of the fetch call at the time abort was called. According to AbortController/abort() docs, "when abort() is called, the fetch() promise rejects with an AbortError."
Change log
- 1.0 Initial release
License
ICS