Added:
trunk/src/jopensocial.deferred.js
Modified:
trunk/src/jopensocial.js
Log:
fetch owner/viewer friends appdata
Added: trunk/src/jopensocial.deferred.js
==============================================================================
--- (empty file)
+++ trunk/src/jopensocial.deferred.js Wed Apr 8 19:22:04 2009
@@ -0,0 +1,45 @@
+;(function(){
+try {
+ var $ = jQuery; $();
+ var opensocial = $.opensocial;
+ var deferred = $.deferred
+} catch(e) { return; }
+
+// _____________private_____________
+
+function deferredize(obj, func, callback_index) {
+ var org_func = obj[func];
+ obj[func] = function() {
+ var d = $.deferred();
+ var deferred_callback = function(data) {
setTimeout(function(){d.call(data);}, 0); };
+ var args = Array.prototype.slice.call(arguments);
+ if(typeof args[callback_index] != "function") args = args.slice(0,
callback_index).concat(null).concat(args.slice(callback_index));
+ if(args[callback_index]) {
+ var callback = args[callback_index];
+ d.next(callback);
+ }
+ args[callback_index] = deferred_callback;
+ org_func.apply(org_func, args);
+ return d;
+ }
+}
+
+// _____________public_____________
+
+var deferred_list = [
+ [$.gadgets, "ajax", 2],
+ [$.gadgets, "getJSON", 2],
+ [$.gadgets, "getFeed", 2],
+ [$.gadgets, "post", 2],
+ [$.opensocial, "getPeople", 2],
+ [$.opensocial, "person", 1],
+ [$.opensocial.data, "get", 2],
+ [$.opensocial.data, "remove", 1],
+ [$.opensocial.data, "set", 2]
+];
+
+$.each(deferred_list, function(k,v) {
+ deferredize.apply(self, v);
+});
+
+})();
Modified: trunk/src/jopensocial.js
==============================================================================
--- trunk/src/jopensocial.js (original)
+++ trunk/src/jopensocial.js Wed Apr 8 19:22:04 2009
@@ -283,52 +283,80 @@
callback(null);
return;
}
- var psn = person(userId,function(psn){
- if(!psn) {
- callback(null); return;
- }
- var id = psn.getId();
- if(useCache && _cachedData[id] && _cachedData[id][key]!=undefined) {
- callback( _cachedData[id][key] );
- return;
+ var multi_key = (key=="*" || key instanceof Array);
+
+ function _decode(v) {
+ var r;
+ try {
+ r =
gadgets.json.parse(decodeURIComponent(gadgets.util.unescapeString(v)));
+ } catch(e) {
+ r= v;
+ }
+ return r;
+ }
+
+ function _setval(k, v, vals) {
+ if(k == "id") { vals[k] = this; return; };
+ _cachedData[k] = _cachedData[k] || {};
+ var val = multi_key?{}:v[key];
//container.mixi?res.userData[key]:res.userData[id][key];
+ if(multi_key) {
+ $.each(v,function(kk, vv){
+ val[kk] = _decode(vv);
+ _cachedData[k][kk] = val[kk];
+ });
+ } else {
+ val = _decode(val);
+ _cachedData[k][key] = val;
}
+ if(vals) vals[k] = val;
+ return val;
+ }
+
+ var ids = userId.match(/^(viewer|owner)_friends$/);
+ if(ids) {
+ var idSpec = opensocial.newIdSpec({userId:ids[1].toUpperCase(),
groupId:"FRIENDS"})
var obj = {
- userData : function() { return
this.newFetchPersonAppDataRequest(opensocial.newIdSpec({userId:id}),[key]);
}
+ peopleData : function() { return
this.newFetchPersonAppDataRequest(idSpec, (key instanceof
Array)?key:[key]); }
}
_request(obj, function(res) {
- if(!res.userData||!res.userData[id]) {
+ if(!res.peopleData||!res.peopleData[id])) {
callback(null);
return;
}
- var data = res.userData[id]||{};
- var val = (key=="*" || key instanceof Array)?{}:data[key];
//container.mixi?res.userData[key]:res.userData[id][key];
- function _decode(v) {
- var r;
- try {
- r = gadgets.json.parse(gadgets.util.unescapeString(v));
- } catch(e) {
- r = v;
- }
- return r;
+ var data = res.peopleData||{};
+ var vals = {};
+ $.each(data, function(k, v) { _setval(k, v, vals) } );
+ callback(vals);
+ });
+ } else {
+ var psn = person(userId,function(psn){
+ if(!psn) {
+ callback(null); return;
+ }
+ var id = psn.getId();
+ if(useCache && _cachedData[id] && _cachedData[id][key]!=undefined) {
+ callback( _cachedData[id][key] );
+ return;
}
- _cachedData[id] = _cachedData[id] || {};
- if(key=="*" || key instanceof Array) {
- $.each(data,function(k){
- val[k] = _decode(this);
- _cachedData[id][k] = val[k];
- });
- } else {
- val = _decode(val);
- _cachedData[id][key] = val;
+ var obj = {
+ userData : function() { return
this.newFetchPersonAppDataRequest(opensocial.newIdSpec({userId:userId.toUpperCase()}),
(key instanceof Array)?key:[key]); }
}
- callback(val);
+ _request(obj, function(res) {
+ if(!res.userData||!res.userData[id]) {
+ callback(null);
+ return;
+ }
+ var data = res.userData[id]||{};
+ var val = _setval(id, data);
+ callback(val);
+ });
});
- });
- var id = psn?psn.getId():null;
- var cache = _cachedData[id];
- if(!cache) return null;
- if(key=="*") return cache;
- return cache[key] || null;
+ var id = psn?psn.getId():null;
+ var cache = _cachedData[id];
+ if(!cache) return null;
+ if(multi_key) return cache;
+ return cache[key] || null;
+ }
}
/**
@@ -439,8 +467,9 @@
* @param userId default VIEWER
* @param opt_params
* @param callback
+ * @param return_object
*/
-function getPeople(userId,opt_params,callback) {
+function getPeople(userId,opt_params,callback, return_object) {
userId = userId || "VIEWER";
var um = userId.match(/^OWNER$|^VIEWER$/i);
if(um) userId = um[0].toUpperCase();
@@ -463,10 +492,10 @@
}
var ar = [];
res.people.each(function(p){
- ar.push(p);
+ if(!return_object) ar.push(p);
_cachedPeople[p.getId()] = p;
});
- callback(ar);
+ callback(return_object?res.people:ar);
});
}