@moos/backbone-fetch

Backbone Models without jQuery.ajax

Usage no npm install needed!

<script type="module">
  import moosBackboneFetch from 'https://cdn.skypack.dev/@moos/backbone-fetch';
</script>

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:

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