changeset 32:0429f7860b30

Moved some UI logic to Config logic.
author Atul Varma <avarma@mozilla.com>
date Sat, 26 Jun 2010 09:23:48 -0700
parents b92bb0974500
children aae9753f12a4
files static-files/index.js
diffstat 1 files changed, 30 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/static-files/index.js	Sat Jun 26 09:10:10 2010 -0700
+++ b/static-files/index.js	Sat Jun 26 09:23:48 2010 -0700
@@ -38,10 +38,35 @@
       if (!('state' in Config.value))
         Config.setValue({state: "login"});
     }
-    
+
     Config.observers.push(ensureStateIsValid);
 
-    $(window).ready(ensureStateIsValid);
+    // This is useful for finding out whether our configuration has
+    // been changed by another instance of our same window.
+    function setupConfigWatcher() {
+      var lastChanged = Config.lastChanged;
+
+      function onConfigChanged() {
+        lastChanged = Config.lastChanged;
+      };
+
+      Config.observers.push(onConfigChanged);
+
+      window.setInterval(
+        function() {
+          if (Config.lastChanged > lastChanged)
+            Config.observers.forEach(function(cb) { cb(); });
+        },
+        1000
+      );
+    }
+
+    function initConfig() {
+      ensureStateIsValid();
+      setupConfigWatcher();
+    }
+
+    $(window).ready(initConfig);
   }
 )(window);
 
@@ -149,27 +174,7 @@
     }
 
     Attendees.observers.push(fillUserInfo);
-
-    function bindConfigToUI() {
-      var lastChanged = Config.lastChanged;
-
-      function onConfigChanged() {
-        lastChanged = Config.lastChanged;
-        updateUI();
-      };
-
-      Config.observers.push(onConfigChanged);
-
-      window.setInterval(
-        function() {
-          if (Config.lastChanged > lastChanged)
-            onConfigChanged();
-        },
-        1000
-      );
-
-      updateUI();
-    }
+    Config.observers.push(updateUI);
 
     function initUI() {
       $(".start-over").submit(
@@ -230,7 +235,7 @@
           {token: verify},
           function(success, data) {
             window.location.hash = "";
-            bindConfigToUI();
+            updateUI();
             if (success) {
               Config.setValue({state: "logged-in",
                                token: data.token,
@@ -241,7 +246,7 @@
             }
           });
       } else
-        bindConfigToUI();
+        updateUI();
     }
 
     $(window).ready(initUI);