changeset 15:a631c77a3bf5

made Config.observers a list, put UI code in a closure.
author Atul Varma <avarma@mozilla.com>
date Thu, 24 Jun 2010 22:46:15 -0700
parents 667ebf2a5e8b
children 9455ec115a5c
files static-files/index.js
diffstat 1 files changed, 31 insertions(+), 27 deletions(-) [+]
line wrap: on
line diff
--- a/static-files/index.js	Thu Jun 24 21:50:40 2010 -0700
+++ b/static-files/index.js	Thu Jun 24 22:46:15 2010 -0700
@@ -15,41 +15,44 @@
     localStorage.setItem("SUMMIT_CFG", JSON.stringify(val));
     localStorage.setItem("SUMMIT_CFG_TIMESTAMP",
                          (new Date()).toString());
-    if (this.observer)
-      this.observer();
+    this.observers.forEach(function(cb) { cb(); });
   },
   wipe: function Config_wipe() {
     localStorage.removeItem("SUMMIT_CFG");
     localStorage.removeItem("SUMMIT_CFG_TIMESTAMP");
-    if (this.observer)
-      this.observer();
+    this.observers.forEach(function(cb) { cb(); });
   },
-  observer: null
+  observers: []
 };
 
-function postJSON(path, obj, cb) {
-  var options = {
-    url: path,
-    type: "POST",
-    contentType: "application/json",
-    data: JSON.stringify(obj),
-    dataType: "json",
-    success: function(data) {
-      cb(true, data);
-    },
-    error: function(req, textStatus, errorThrown) {
-      var data = null;
-      if (req.getResponseHeader("Content-Type") == "application/json") {
-        try {
-          data = JSON.parse(req.responseText);
-        } catch (e) {}
+var Api = {
+  postJSON: function Api_postJSON(path, obj, cb) {
+    var options = {
+      url: path,
+      type: "POST",
+      contentType: "application/json",
+      data: JSON.stringify(obj),
+      dataType: "json",
+      success: function(data) {
+        cb(true, data);
+      },
+      error: function(req, textStatus, errorThrown) {
+        var data = null;
+        if (req.getResponseHeader("Content-Type") == "application/json") {
+          try {
+            data = JSON.parse(req.responseText);
+          } catch (e) {}
+        }
+        cb(false, data);
       }
-      cb(false, data);
-    }
-  };
-  return jQuery.ajax(options);
-}
+    };
+    return jQuery.ajax(options);
+  }
+};
 
+// This anonymous closure sets up the UI.
+
+(function(window) {
 function ensureState() {
   if (!('state' in Config.value))
     Config.setValue({state: "login"});
@@ -85,7 +88,7 @@
     updateUI();
   };
 
-  Config.observer = onConfigChanged;
+  Config.observers.push(onConfigChanged);
 
   window.setInterval(
     function() {
@@ -141,3 +144,4 @@
     } else
       bindConfigToUI();
   });
+})(window);