Git Product home page Git Product logo

backbone-ajaxretry's Introduction

Backbone.js ajaxRetry

Exponentially retry Backbone.ajax and $.ajax requests

 

Installation

npm install --save backbone-ajaxretry

 
In your client app main.js, add the following line to default retry settings:

require('backbone-ajaxretry');

Or override any of the default settings using set: passing keyword arguments

require('backbone-ajaxretry').set({ y:0.3 });

 

Usage

The default settings are:

{
  base: 2.718281828,
  y: 0.25,
  retryCount: 3,
  onlyBackbone: false
}

 

By default both Backbone.ajax and $.ajax Server Errors are retried. To only retry Backbone.ajax requests and not also [regular] $.ajax requests, change the onlyBackbone default setting to true.

For Backbone.js sync, fetch, save or destroy, pass exhaust in the options object as a callback function to run when retries fail

  • please note that exhaust supersedes the error callback
  • if exhaust method is not passed, retries will end without further action
  • the returned jqXHR object has been extended with the ajax request options,
    thus allowing jqXHR.type, jqXHR.url, etcetera
// Backbone ex.
myModel.fetch({
  exhaust : function (jqXHR, textStatus, errorThrown) {
    // Handle Internal Server Error
  }
});

// $.ajax ex.
$.ajax({
  url: '/test',
  type: 'GET',
  exhaust : function (jqXHR, textStatus, errorThrown) {
    // Handle Internal Server Error
  }
});

 




backbone-ajaxretry's People

Contributors

gdibble avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

backbone-ajaxretry's Issues

Uncaught TypeError: Cannot read property 'ajax' of undefined

Users have reported JS error.

Temporary fix (until this is resolved) includes adding the following first-line to the Backbone.ajax extend-function before the return:

  Backbone.$ = $; // <<< add as temp-fix
  return Backbone.$.ajax.apply(Backbone.$, args);

bug in extender fn

apologies w/ v1.2.0. when using $.get or $.post shortcuts, first argument is a string and thus internal extender function had a bug.

POST response 200 w/o body still retries

a POST with a response-body, PUT, GET, DELETE all work as expected,
but a POST with res.status 200 and no response-body keeps retrying the successful req.

considering checking jqXHR.status < 300 >>> _exhausted()

ability to overload settings per instantiation

For example, in my app I want to use the default of { retryCount:3 } in most cases,
but this particular fetch I would like to try more or less, i.e.

myModel1.fetch({
  retryCount: 99
});
// ||
myModel2.fetch({
  retryCount: 0
});

Any setting, default or via .set, should allow overloading (thus superseding settings) from the current instantiation.

minify main js file

provide compress library for Backbone apps requiring library on-the-fly and developers who include this module via HTML or lazy-loading.

initial estimate shows 51% reduction.

pass error arguments to exhaust

$.ajax returns arguments to the error callback:
( jqXHR jqXHR, String textStatus, String errorThrown )

These arguments should be passed forward to the exhaust callback such that the developer can do something like:

function onExhaust(jqXHR, textStatus, errorThrown) {
  console.log(jqXHR, textStatus, errorThrown);
  if (jqXHR.status > 500) {
    //do something about Server Error
  } else if (jqXHR.status > 400) {
    //do something about Client Error
  }
}

myModel.fetch({ exhaust:onExhaust });

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.