Skip to content

Instantly share code, notes, and snippets.

@duhast
Created September 24, 2014 15:43
Show Gist options
  • Save duhast/47454b880dbf72344b32 to your computer and use it in GitHub Desktop.
Save duhast/47454b880dbf72344b32 to your computer and use it in GitHub Desktop.
AJAX request indicator (CoffeeScript, BackboneJS, jQuery)
$(document).ready ->
myApp.loader = new myApp.views.LoadingIndicator()
$(document).ajaxSend -> myApp.loader.start()
$(document).ajaxComplete -> myApp.loader.finish()
$(document).ajaxError -> myApp.loader.finish()
#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;
}
}
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