-
Notifications
You must be signed in to change notification settings - Fork 2
/
backbone-ajaxretry.js
114 lines (92 loc) · 3.11 KB
/
backbone-ajaxretry.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*global define*/
/*!
* Backbone.js ajaxRetry
* https://github.com/gdibble/backbone-ajaxretry
* Copyright 2015 Gabriel Dibble; Licensed MIT
*/
var defaults = require('lodash.defaults');
var extend = require('extend-object');
(function (define) { define(function (require, exports, module) { //'use strict';
var Backbone = require('backbone');
var settings = { //Defaults that can be overridden via set
base: 2.718281828,
y: 0.25,
retryCount: 3,
onlyBackbone: false
};
var _$ajax;
//-----------------------------------------------------------------------------
// Helpers:
// some console.logs have been left (as to be enabled) for those curious minds
//Update current settings, overriding defaults
function setOptions(options) {
defaults(options, settings);
settings = options;
}
//delay in milliseconds
function exponentialDelay(x) {
return (Math.pow(settings.base, x) - settings.y) * 1000;
}
//hit retry limit
function exhausted() {
var args = Array.prototype.slice.call(arguments, 0);
extend(args[0], this);
// console.log('exhausted', this.url);
if (this.hasOwnProperty('exhaust')) {
// console.log('>>> called this.exhaust', this.exhaust);
this.exhaust.apply(this, args);
}
}
//recurse the ajax request
function ajaxRetry(jqXHR) {
var self = this;
if (this.hasOwnProperty('retries')) {
this.recursed = this.recursed === undefined ? 0 : this.recursed + 1;
if ((jqXHR && jqXHR.status < 500) || this.recursed >= this.retries) {
exhausted.apply(self, arguments);
} else if (this.recursed < this.retries) {
setTimeout(function () {
$.ajax(self);
// console.log('recursed', self.url, self.recursed, 'in ' + exponentialDelay(self.recursed) + 'ms');
}, exponentialDelay(this.recursed));
}
} else {
exhausted.apply(self, arguments);
}
}
function extender(args, options) {
extend(args[0], options && typeof options === 'object' ? options : {}, {
retries: settings.retryCount,
error: function () { ajaxRetry.apply(this, arguments); }
});
}
function sliceArguments() {
return Array.prototype.slice.call(arguments, 0);
}
//-----------------------------------------------------------------------------
//extend for retry functionality:
if (!settings.onlyBackbone) {
//retry regular $.ajax thus also Backbone ajax requests
_$ajax = $.ajax;
$.ajax = function (options) {
var args;
if (typeof options === 'string') {
arguments[1].url = options; //in this case, options is actually the url passed to $.get/$.post
args = sliceArguments(arguments[1]);
extender(args, options);
} else {
args = sliceArguments(arguments);
extender(args, options);
}
return _$ajax.apply($, args);
};
} else {
//retry only Backbone ajax requests422
Backbone.ajax = function (options) {
var args = sliceArguments(arguments);
extender(args, options);
return Backbone.$.ajax.apply(Backbone.$, args);
};
}
module.exports = { set: setOptions };
}); }(typeof define === 'function' && define.amd ? define : function (factory) { factory(require, exports, module); })); //end UMD CommonJS wrapper