1.0.0 • Published 7 years ago

backbone-es6-promise v1.0.0

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

Backbone ES6 promise

A plugin for Backbone.js that overrides default promises with modern ES6 promises.

Build Status

Backbone ES6 promise is the simple plugin that overrides old, not trendy jQuery promises with new, awesome ES6 promises. With this tool all your request methods of models and collections will return ES6 promises.

How to install:

npm install backbone-es6-promise --save

Browser:

<script>...</script>
<!-- add polyfill if you want to support old browsers -->
<script src='promise-polyfill/promise.js'></script>
<script src='backbone-es6-promise.js' type='text/javascript'></script>

Common JS:

require('backbone-es6-promise');

How to use:

Just start to write your code with ES6 promise syntax.

const Model = Backbone.Model.extend({ urlRoot: 'url' });
const model = new Model({ id: 1, someAttribute: 'some attribute' });

model.fetch().then(data => {
  console.log(data);
}).catch(errorObject => {
  console.log(errorObject);
});

Also you are able to write your custom promises in same style with Backbone.Promise function. Don't worry, no magic here, it is just an alias for ES6 Promise function.

const promise = new Backbone.Promise((resolve, reject) => {
  // do a something async
  if (/* everything turned out fine */) {
    resolve('Stuff worked!');
  } else {
    reject(Error('It broke'));
  }
});