Created
March 28, 2013 03:57
-
-
Save justinobney/5260476 to your computer and use it in GitHub Desktop.
Web App Working Bar - Like GMail
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
var WorkerBar = (function(){ | |
var promises = []; | |
var workingbar = $('.working'); | |
var done; | |
var add = function(promise) { | |
if ( promises.length == 0 ){ | |
showWorking(); | |
} | |
// push new promise | |
promises.push(promise); | |
// re-register the when | |
$.when.apply($, promises).done(function(){ | |
var doneCount = _.filter(promises, function(e){ | |
return e.state() === "resolved" | |
}).length; | |
if (doneCount === promises.length) { | |
hideWorking(); | |
} | |
}); | |
} | |
var showWorking = function(){ | |
workingbar.show(); | |
} | |
var hideWorking = function(){ | |
workingbar.hide(); | |
promises = []; | |
} | |
return { | |
Add: add | |
} | |
})(); | |
var a = jQuery.Deferred(); | |
var b = jQuery.Deferred(); | |
setTimeout(function(){ | |
console.log('faking ajax request 1') | |
WorkerBar.Add(a.promise()); | |
},1000); | |
a.done(function(){ | |
console.log('request 1 is done') | |
}); | |
b.done(function(){ | |
console.log('request 2 is done') | |
}); | |
setTimeout(function(){ | |
console.log('faking ajax request 2') | |
WorkerBar.Add(b.promise()); | |
},2000); | |
setTimeout(function(){ | |
a.resolve() | |
},2000); | |
setTimeout(function(){ | |
b.resolve() | |
},4000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment