Refactor javascript code for fallback thumbnail, use something more simplier #1521
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.
So this was an issue I was fighting with for awhile on the webpacker PR.
The problem is we want this
default_thumbnail()
method setup as a global method and exposed to the entire app (e.g you can call it from browser console, or any html). This goes against Webpackers defaults and is a major pain to get something like this to work in webpacker. I tried to replace this with jquery and watching for.error/.load
events on the image but it works half the time (works on turbolinks page loads, but on a hard refresh it fails as image fails to load before javascript loads and fires). Also looked into using a library like https://github.com/desandro/imagesloaded but didn't get much success either. Other option is opening a<script></script
tag and throwing this function inside to by pass webpacker...but thats messy as well.Instead of spending a ton of time on this, I just took a simple approach. Just fallback to the ERA logo without any text on it. We do this for community thumbnails already, so figured this be okay here as well.
Tested this and it all works quite nicely. If we cannot load an image we just get the ERA logo without text as such: