@moos/backbone-fetch

1.0.2 • Public • Published

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:

Versioned (recommemded in production):

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

Package Sidebar

Install

npm i @moos/backbone-fetch

Weekly Downloads

0

Version

1.0.2

License

ISC

Unpacked Size

19.9 kB

Total Files

4

Last publish

Collaborators

  • moos