Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix retry not preserving original ajax options if context is specified #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions dist/jquery.ajax-retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,17 @@
if (opts.statusCodes) {
this.statusCodes = opts.statusCodes;
}
return this.pipe(null, pipeFailRetry(this, opts));
return this.pipe(null, pipeFailRetry(this, opts, originalOptions));
};
});

// generates a fail pipe function that will retry `jqXHR` `times` more times
function pipeFailRetry(jqXHR, opts) {
function pipeFailRetry(jqXHR, opts, ajaxOptions) {
var times = opts.times;
var timeout = jqXHR.timeout;

// takes failure data as input, returns a new deferred
return function(input, status, msg) {
var ajaxOptions = this;
var output = new $.Deferred();
var retryAfter = jqXHR.getResponseHeader('Retry-After');

Expand Down
2 changes: 1 addition & 1 deletion dist/jquery.ajax-retry.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions src/jquery.ajax-retry.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,17 @@
if (opts.statusCodes) {
this.statusCodes = opts.statusCodes;
}
return this.pipe(null, pipeFailRetry(this, opts));
return this.pipe(null, pipeFailRetry(this, opts, originalOptions));
};
});

// generates a fail pipe function that will retry `jqXHR` `times` more times
function pipeFailRetry(jqXHR, opts) {
function pipeFailRetry(jqXHR, opts, ajaxOptions) {
var times = opts.times;
var timeout = jqXHR.timeout;

// takes failure data as input, returns a new deferred
return function(input, status, msg) {
var ajaxOptions = this;
var output = new $.Deferred();
var retryAfter = jqXHR.getResponseHeader('Retry-After');

Expand Down
18 changes: 18 additions & 0 deletions test/jquery.ajax-retry_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,5 +270,23 @@
'{ "id": 12, "comment": "error!" }');

});

asyncTest('ajax options and context are preserved', 4, function() {
var testContextObject = { someContext:"forTest" };
var def = $.ajax({url:"/test",data:{},type:"POST",context:testContextObject});
def.retry({times:2}).then(function(data){
ok(data.id === 12);
strictEqual(this, testContextObject);
start();
});
this.requests[0].respond(400, { "Content-Type": "application/json" },
'{ "id": 13, "comment": "error!" }');
this.requests[1].respond(200, { "Content-Type": "application/json" },
'{ "id": 12, "comment": "Hey there" }');

ok(this.requests[0].method === 'POST');
ok(this.requests[1].method === 'POST');

});

}(jQuery));