Allow for HTML swaps even on AJAX Errors #346
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This was a crude implementation of the newer HTMX response targets extension but only for errors and it cannot distinguish the error codes like response targets can.
I wanted to seamlessly push the 404 html fragment into the main view however that would have required a duplicated bunch of nonsense using ic-on-error with jQuery like
ic-on-error="$('#view').html(xhr.responseText)
and I thought well rather then do that why not add another attribute to IC to tell it to push an html swap even if the ajax has entered a failed state, and this is that implementation.I've tested this on IE (10 on Windows 8 and 11 via MS Edge) aswell as Firefox 22+, Opera 21+ and Chrome 31+ (I'm using IC.js to build a site that has to run on relatively ancient browsers from about 2014 onwards so I made sure it ran without a hitch)
Here's an example of how to use it (simplified snippet from the production app):
One thing that could be improved is being able to set ic-swap-on-error on the target rather then the nearest element however it took quite a while to grasp how to even pull this off so that's a later problem.