Skip to content

Commit

Permalink
reject when load has an "error" status
Browse files Browse the repository at this point in the history
This allows for a callback to be registered with the promise to handle
errors like a 404. Without calling reject, the promise is always in a
pending state.
  • Loading branch information
William Wedler authored and marcandre committed May 19, 2018
1 parent c4ed123 commit aca473f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/featherlight.js
Expand Up @@ -282,9 +282,11 @@

/* Set content and show */
return $.when($content)
.always(function($content){
self.setContent($content);
self.afterContent(event);
.always(function($openendContent){
if($openendContent) {
self.setContent($openendContent);
self.afterContent(event);
}
})
.then(self.$instance.promise())
/* Call afterOpen after fadeIn is done */
Expand Down Expand Up @@ -395,7 +397,7 @@
if ( status !== "error" ) {
deferred.resolve($container.contents());
}
deferred.fail();
deferred.reject();
});
return deferred.promise();
}
Expand Down
12 changes: 12 additions & 0 deletions test/featherlight_test.js
Expand Up @@ -282,6 +282,18 @@ var stubAjaxLoad = function(content) {
});
});

it('can recover from an ajax load error, like a 404', function(done) {
var modal = new $.featherlight({ajax: 'bad-url-not-found.html'});
var p = modal.open();
var callbackCalled = false;
p.fail(function() {
callbackCalled = true;
})
patiently(done, function() {
expect(callbackCalled).to.be.true;
});
});

it('ajax content can be text only', function(done) {
stubAjaxLoad('Hello <b>world</b>');
$.featherlight({ajax: 'stubbed'});
Expand Down

0 comments on commit aca473f

Please sign in to comment.