Advertisement
Guest User

Untitled

a guest
May 27th, 2015
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. var PackageLoader = function() {
  2. this.cb = Math.random(0, 100000000000);
  3. this.current = 0;
  4. this.batches = [];
  5.  
  6. // Load kicks off the entire loading process.
  7. this.load = function(config, loadMap, onload) {
  8. var scope = this;
  9. for(label in loadMap) {
  10. scope.batches.push(new PackageBatch(label, loadMap[label]));
  11. }
  12. scope.onload = onload;
  13. if (config.preloader && config.jQuery) {
  14. requirejs([
  15. config.preloader,
  16. config.jQuery
  17. ], function() {
  18. PackagePreloader.instance.init(config);
  19. scope._subLoad();
  20. });
  21. } else {
  22. console.error("PackageLoader.load: First argument (config object) requires 'preloader' and 'jQuery' source path properties, or the function cannot run.");
  23. }
  24. };
  25.  
  26. // This loads the current (not-yet-loaded) batch of packages.
  27. this._subLoad = function() {
  28. PackagePreloader.instance.update();
  29. var scope = this;
  30. if (this.current<this.batches.length) {
  31. this.batches[this.current].require(function() {
  32. scope.current++;
  33. scope._subLoad();
  34. });
  35. } else {
  36. $(function() {
  37. PackagePreloader.instance.complete(scope.onload);
  38. });
  39. }
  40. };
  41.  
  42. // Returns the current package. The point of this is to be a neat public function.
  43. this.getCurrentPackage = function() {
  44. return this.batches[this.current];
  45. };
  46. };
  47.  
  48. // This is currently automatically a singleton, until a better method of defining the PackageLoader/PackagePreloader relationship is defined.
  49. PackageLoader.instance = new PackageLoader();
  50.  
  51. // Handles a single 'batch' of packages.
  52. var PackageBatch = function(label, batch) {
  53. this.label = label;
  54. this.batch = batch;
  55.  
  56. this.require = function(complete) {
  57. requirejs(this.batch, complete);
  58. };
  59.  
  60. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement