Last active
July 13, 2023 08:37
-
-
Save brian-mann/7085079 to your computer and use it in GitHub Desktop.
Expanded version of loading controller with edge cases covered
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
showRealView: (realView, loadingView, config) -> | |
xhrs = App.request "fetched:entities", config.entities | |
## ...after the entities are successfully fetched, execute this callback | |
$.when(xhrs...).done => | |
## ================================================================ ## | |
## If the region we are trying to insert is not the loadingView then | |
## we know the user has navigated to a different page while the loading | |
## view was still open. In that case, we know to manually close the original | |
## view so its controller is also closed. We also prevent showing the real | |
## view (which would snap the user back to the old view unexpectedly) | |
## ================================================================ ## | |
switch config.loadingType | |
when "opacity" | |
@region.currentView.$el.removeAttr "style" if @region.currentView | |
when "spinner" | |
return realView.close() if @region.currentView isnt loadingView | |
## show the real view unless we've set debug in the loading options | |
@show realView unless config.debug | |
config.done(realView) | |
$.when(xhrs...).fail => | |
## we want to close the realView if we've aborted an XHR request | |
## this ensures any instantiated views not yet inside of a region are properly closed | |
## we're allowing aborting because if there are multiple AJAX requests going on we | |
## run the risk of older requests finishing later (race condition) and then | |
## updating the view to an older incorrect request's view | |
realView.close() unless @region.currentView is realView | |
## also need to handle any error messaging to the client here | |
## --my implementation for that (project specific) -- | |
## ...after the entities are finished regardless of success or failure | |
$.when(xhrs...).always => | |
## we always want to close the loadingView if it exists | |
loadingView?.close() | |
## and close out the loading controller since its no longer needed regardless | |
@close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment