Created
September 24, 2014 15:43
-
-
Save duhast/47454b880dbf72344b32 to your computer and use it in GitHub Desktop.
AJAX request indicator (CoffeeScript, BackboneJS, jQuery)
This file contains hidden or 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
$(document).ready -> | |
myApp.loader = new myApp.views.LoadingIndicator() | |
$(document).ajaxSend -> myApp.loader.start() | |
$(document).ajaxComplete -> myApp.loader.finish() | |
$(document).ajaxError -> myApp.loader.finish() |
This file contains hidden or 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
#loading { | |
position: fixed; | |
top: 10px; | |
left: 8px; | |
width: 106px; | |
height: 30px; | |
background: white; | |
color: #22668b; | |
opacity: 0.6; | |
border-radius: 5px; | |
padding-top: 6px; | |
padding-left: 14px; | |
span { | |
padding-left: 3px; | |
padding-top: 2px; | |
} | |
} |
This file contains hidden or 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
class myApp.views.LoadingIndicator extends Backbone.View | |
counter: 0 | |
delay: 500 | |
lastServerAction: null | |
constructLoader: -> | |
# Uses FontAwesome icon | |
$('<div id="loading"><i class="fa fa-lg fa-refresh fa-spin"></i> <span>Loading...</span></div>') | |
render: -> | |
@$loader = @constructLoader() | |
$('body').append(@$loader) | |
@$loader.hide() | |
start: -> | |
@render() unless @$loader | |
@lastServerAction = new Date() | |
@$loader.show() if @counter is 0 | |
++@counter | |
finish: -> | |
return unless @$loader | |
--@counter if @counter > 0 | |
setTimeout (=> @$loader.hide('fast')), @delay if @counter is 0 | |
reset: -> @counter = 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment