Not waiting for settings.json for production files to load; BootManager
authorDarren <darren@darrenwhitlen.com>
Fri, 25 Oct 2013 21:19:23 +0000 (22:19 +0100)
committerDarren <darren@darrenwhitlen.com>
Fri, 25 Oct 2013 21:19:23 +0000 (22:19 +0100)
client/assets/src/index.html.tmpl

index 83ace19620b9c7fae34e7abef1e5d567e039be78..a0940524caf6daed6aef7160195e7a98acbd726a 100644 (file)
 \r
         normalizeConsole();\r
 \r
+        /**\r
+         * Job bootup manager\r
+         * Once all jobs have completed, call any registered completed functions\r
+         */\r
+        var jobs = new (function BootManager() {\r
+            var completed_jobs = {  };\r
+\r
+            // Functions to call once all jobs have completed\r
+            var completed_callbacks = [];\r
+\r
+\r
+            function callCompletedFunctions(fn) {\r
+                $.each(completed_callbacks, function(idx, fn) {\r
+                    fn();\r
+                });\r
+\r
+                completed_callbacks = [];\r
+            };\r
+\r
+\r
+            this.finishJob = function(job_name) {\r
+                if (typeof completed_jobs[job_name] === 'undefined') {\r
+                    return;\r
+                }\r
+\r
+                completed_jobs[job_name] = true;\r
+\r
+                // Check if all our jobs have completed\r
+                var all_jobs_completed = true;\r
+                $.each(completed_jobs, function(idx, completed) {\r
+                    if (!completed) {\r
+                        all_jobs_completed = false;\r
+                        return false;\r
+                    }\r
+                });\r
+\r
+                if (all_jobs_completed) {\r
+                    callCompletedFunctions();\r
+                }\r
+            };\r
+\r
+\r
+            this.onFinish = function(fn) {\r
+                completed_callbacks.push(fn);\r
+            };\r
+\r
+\r
+            this.registerJob = function(job_name) {\r
+                completed_jobs[job_name] = false;\r
+            };\r
+        })();\r
+\r
+\r
         // Run after all dependancies have been loaded\r
-        function startApp () {\r
+        jobs.onFinish(function startApp() {\r
             // Start the app\r
             kiwi.start(opts, function() {\r
                 // Load any plugins\r
                     });\r
                 }\r
             });\r
-        }\r
+        });\r
 \r
 \r
         // Load each script\r
+        jobs.registerJob('load_scripts');\r
         var cur_script = 0;\r
         function loadNextScript () {\r
             var to_load,\r
 \r
             // Start the kiwi app if all scripts have been loaded\r
             if (cur_script === scripts.length) {\r
-                startApp();\r
+                jobs.finishJob('load_scripts');\r
                 return;\r
             }\r
 \r
             cur_script++;\r
         }\r
 \r
+        // If we're not interested in debug libs, start loading production files\r
+        if (!getQueryVariable('debug')) {\r
+            scripts.push(['libs/lodash.min.js']);\r
+            scripts.push([\r
+                'libs/backbone.min.js',\r
+                'libs/jed.js'\r
+            ]);\r
+            scripts.push([\r
+                'kiwi.min.js',\r
+                'libs/engine.io.bundle.min.js'\r
+            ]);\r
+\r
+            loadNextScript();\r
+        }\r
+\r
         // Load application settings\r
+        jobs.registerJob('load_settings');\r
         $.getJSON(opts.settings_path, function (data) {\r
-            scripts = scripts.concat(data.scripts);\r
             opts.server_settings = data.server_settings;\r
             opts.client_plugins = data.client_plugins;\r
             opts.translations = data.translations;\r
             if (typeof data.kiwi_server !== 'undefined')\r
                 opts.kiwi_server = data.kiwi_server;\r
 \r
-            // Start loading scripts\r
-            loadNextScript();\r
+            jobs.finishJob('load_settings');\r
+\r
+            // If debugging, grab the debug scripts and load them\r
+            if (getQueryVariable('debug')) {\r
+                scripts = scripts.concat(data.scripts);\r
+                loadNextScript();\r
+            }\r
         });\r
     };\r
 </script>\r