Author: doll
Date: Tue May  5 17:56:10 2009
New Revision: 771945

URL: http://svn.apache.org/viewvc?rev=771945&view=rev
Log:
opensocial templates now handle errors in response items. 
patch created with Lev Epshteyn


Modified:
    
incubator/shindig/trunk/features/src/main/javascript/features/opensocial-data-context/datacontext.js
    
incubator/shindig/trunk/features/src/main/javascript/features/opensocial-data/data.js

Modified: 
incubator/shindig/trunk/features/src/main/javascript/features/opensocial-data-context/datacontext.js
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/features/src/main/javascript/features/opensocial-data-context/datacontext.js?rev=771945&r1=771944&r2=771945&view=diff
==============================================================================
--- 
incubator/shindig/trunk/features/src/main/javascript/features/opensocial-data-context/datacontext.js
 (original)
+++ 
incubator/shindig/trunk/features/src/main/javascript/features/opensocial-data-context/datacontext.js
 Tue May  5 17:56:10 2009
@@ -39,40 +39,16 @@
    * Puts a data set into the global DataContext object. Fires listeners
    * if they are satisfied by the associated key being inserted.
    *
-   * Note that if this is passed a ResponseItem object, it will crack it open
-   * and extract the JSON payload of the wrapped API Object. This includes
-   * iterating over an array of API objects and extracting their JSON into a
-   * simple array structure.
-   *
    * @param {string} key The key to associate with this object.
    * @param {ResponseItem|Object} obj The data object.
    * @param {boolean} opt_fireListeners Default true.
    */
   var putDataSet = function(key, obj, opt_fireListeners) {
-    var data = obj;
-    if (typeof data === 'undefined' || data === null) {
+    if (typeof obj === 'undefined' || obj === null) {
       return;
     }
   
-    // NOTE: This is ugly, but required since we need to get access
-    // to the JSON/Array payload of API responses.
-    // This will crack the actual API objects and extract their JSON payloads.
-    // TODO: this code block is not described by the spec, and should be 
removed.
-    // Developers using ResponseItems should call getData() themselves.
-    if (data.getData) {
-     data = data.getData();
-     if (data.array_) {
-       var out = [];
-       for (var i = 0; i < data.array_.length; i++) {
-         out.push(data.array_[i].fields_);
-       }
-       data = out;
-     } else {
-       data = data.fields_ || data;
-     }
-    }
-  
-    dataSets[key] = data;
+    dataSets[key] = obj;
     if (!(opt_fireListeners === false)) {
       fireCallbacks(key);
     }
@@ -182,20 +158,12 @@
   return {
     
     /**
-     * Returns a map of existing data. This is used externally by both the
-     * opensocial-data and opensocial-templates feature, hence is
-     * not hidden, despite not being part of the spec.
+     * Returns a map of existing data.
      * @return {Object} A map of current data sets.
      * TODO: Add to the spec API?
      */
     getData : function() {
-      var data = {};
-      for (var key in dataSets) {
-        if (dataSets.hasOwnProperty(key)) {
-          data[key] = dataSets[key];
-        }
-      }
-      return data;
+      return dataSets;
     },
     
     /**
@@ -263,4 +231,3 @@
 opensocial.data.getDataContext = function() {
   return opensocial.data.DataContext;
 };
-

Modified: 
incubator/shindig/trunk/features/src/main/javascript/features/opensocial-data/data.js
URL: 
http://svn.apache.org/viewvc/incubator/shindig/trunk/features/src/main/javascript/features/opensocial-data/data.js?rev=771945&r1=771944&r2=771945&view=diff
==============================================================================
--- 
incubator/shindig/trunk/features/src/main/javascript/features/opensocial-data/data.js
 (original)
+++ 
incubator/shindig/trunk/features/src/main/javascript/features/opensocial-data/data.js
 Tue May  5 17:56:10 2009
@@ -341,18 +341,50 @@
  * @param {Object<string, Function(string, ResponseItem)>} callbacks A map of
  * any custom callbacks by key.
  */
-opensocial.data.onAPIResponse = function(data, keys, callbacks) {
+opensocial.data.onAPIResponse = function(responseItem, keys, callbacks) {
   for (var i = 0; i < keys.length; i++) {
     var key = keys[i];
-    var item = data.get(key);
-    if (callbacks[key]) {
-      callbacks[key](key, item);
-    } else {
-      opensocial.data.DataContext.putDataSet(key, item);
+    var item = responseItem.get(key);
+    if (!item.hadError()) {
+      var data = opensocial.data.extractJson_(item, key);
+      if (callbacks[key]) {
+        callbacks[key](key, data);
+      } else {
+        opensocial.data.DataContext.putDataSet(key, data);
+      }
     }
+    // TODO: What should we do if there *is* an error?
   }
 };
 
+/**
+ * Extract the JSON payload from the ResponseItem. This includes
+ * iterating over an array of API objects and extracting their JSON into a
+ * simple array structure.
+ */
+opensocial.data.extractJson_ = function(responseItem, key) {
+  var data = responseItem.getData();
+  if (data.array_) {
+    var out = [];
+    for (var i = 0; i < data.array_.length; i++) {
+      out.push(data.array_[i].fields_);
+    }
+    data = out;
+    
+    // For os:PeopleRequests that request @groupId="self", crack the array
+    var request = opensocial.data.requests_[key];
+    if (request.tagName == "os:PeopleRequest") {
+      var groupId = request.getAttribute("groupId");
+      if ((!groupId || groupId == "@self") && data.length == 1) {
+        data = data[0]
+      }
+    }
+  } else {
+    data = data.fields_ || data;
+  }
+  return data;
+};
+
 
 /**
  * Registers a tag as a data request handler.
@@ -569,4 +601,4 @@
   if (window["gadgets"] && gadgets.util.hasFeature("views")) {
     opensocial.data.DataContext.putDataSet("ViewParams", 
gadgets.views.getParams());
   }
-})();
\ No newline at end of file
+})();


Reply via email to