メモ代わり。てきとーに。 いや、ですからてきとーですって。 2年前ぐらいにPythonあたりでメールくれた方、ごめんなさい。メール紛失してしまい無視した形になってしまいました。。。

2009年7月29日水曜日

[Apache Shindig][お勉強][OpenSocial] メモ101 requestShareAppを動かす

なんだかshindig-1.1-SNAPSHOTだと動かないっぽいので、
動かす。

まず、jsonrpccontainer.jsを見てみる。

  1. JsonRpcContainer.prototype.requestShareApp = function(recipientIds, reason,  
  2.     opt_callback, opt_params) {  
  3.   var callbackId = "cId_" + Math.random();  
  4.   callbackIdStore[callbackId] = opt_callback;  
  5.   
  6.   var body = gadgets.util.unescapeString(reason.getField(  
  7.       opensocial.Message.Field.BODY));  
  8.   
  9.   if (!body || body.length === 0) {  
  10.     var bodyMsgKey = gadgets.util.unescapeString(reason.getField(  
  11.       opensocial.Message.Field.BODY_ID));  
  12.     body = gadgets.Prefs.getMsg(bodyMsgKey);  
  13.   }  
  14.   
  15.   gadgets.rpc.call('..''shindig.requestShareApp',  
  16.       null,  
  17.       callbackId,  
  18.       recipientIds,  
  19.       body);  
  20. };  

opensocial.requestShareAppがコールされると、この関数がコールされる。
関数の最後にgadgets.rpc.callで、親の'shindig.requestShareApp'がコールされている。

ということで、今度は、gadgets.jsを見てみる。
  1. gadgets.IfrGadgetService = function() {  
  2.   var self = this;  
  3.   gadgets.GadgetService.call(this);  
  4.   gadgets.rpc.register('resize_iframe'this.setHeight);  
  5.   gadgets.rpc.register('set_pref'this.setUserPref);  
  6.   gadgets.rpc.register('set_title'this.setTitle);  
  7.   gadgets.rpc.register('requestNavigateTo'this.requestNavigateTo);  
  8. };  

となっていて、shindig.requestShareAppの登録はされていない。
ま、そりゃそうか。

jsonrpccontainer.jsも、gadgets.jsも、本来自分で実装すべきところっぽい。
だからサンプルが動く程度の実装しかされていないんで、「追加」という形で実装してみる。

呼び出し元のパラメータは
  1. gadgets.rpc.call('..''shindig.requestShareApp',  
  2.     null,  
  3.     callbackId,  
  4.     recipientIds,  
  5.     body);  

となっているので、
gadgets.rpc.registerで記述すべきは、
  1.   gadgets.rpc.register('shindig.requestShareApp'function(callbackId,recipientIds,body) {  
  2.   self.requestShareApp.apply(self,[this, callbackId, recipientIds, body]);  
  3. });  

と、こんな感じか。

でrequestShareAppの実装。
  1. gadgets.IfrGadgetService.prototype.shareAppDialog_ = null;  
  2. gadgets.IfrGadgetService.prototype.setShareAppDialog = function(dialog) {  
  3.   gadgets.container.gadgetService.shareAppDialog_ = dialog;  
  4. };  
  5. gadgets.IfrGadgetService.prototype.requestShareApp = function(rpc, callbackId, recipients, body) {  
  6.     if (gadgets.container.gadgetService.shareAppDialog_) {  
  7.       /* 以下YUI前提 */  
  8.       var onSubmit = function() {  
  9.         if (callbackId) {  
  10.           window.setTimeout(function() {  
  11.             gadgets.rpc.call(rpc.f, 'shindig.requestShareApp_callback'null, callbackId, true, recipients, body);  
  12.           }, 0);  
  13.         }  
  14.         this.cancel();  
  15.       };  
  16.       var onCancel = function() {  
  17.         if (callbackId) {  
  18.           window.setTimeout(function() {  
  19.             gadgets.rpc.call(rpc.f, 'shindig.requestShareApp_callback'null, callbackId, false, recipients, body);  
  20.           }, 0);  
  21.         }  
  22.         this.cancel();  
  23.       };  
  24.       gadgets.container.gadgetService.shareAppDialog_.cfg.queueProperty("buttons", [  
  25.         {text: "送信", handler: onSubmit, isDefault: true},  
  26.         {text: "キャンセル", handler: onCancel}  
  27.       ]);  
  28.       document.getElementById('shareAppDialog_body').innerHTML = body;  
  29.       gadgets.container.gadgetService.shareAppDialog_.render();  
  30.       gadgets.container.gadgetService.shareAppDialog_.show();  
  31.     }  
  32.     else {  
  33.       if (callbackId) {  
  34.         window.setTimeout(function() {  
  35.           gadgets.rpc.call(rpc.f, 'shindig.requestShareApp_callback'null, callbackId, true, recipients, body);  
  36.         }, 0);  
  37.       }  
  38.     }  
  39. };  

と、ざっくりこんな感じ。馬鹿の一つ覚えでYUIをまた使用。
親側でダイアログを出して、子フレーム側のrequestShareApp_callbackをコールできればOK。
shindigのままだと、bodyをコールバックに渡せないので、
jsonrpccontainer.jsの法をちょっとだけいじる。
  1. JsonRpcContainer.requestShareAppCallback_ = function(callbackId,  
  2.     success, recipientIds, body) {  
  3.   callback = callbackIdStore[callbackId];  
  4.   if (callback) {  
  5.     callbackIdStore[callbackId] = null;  
  6.   
  7.     var data = null;  
  8.     if (recipientIds) {  
  9.       data = {'recipientIds': recipientIds};  
  10.     }  
  11.   
  12.     var responseItem = new opensocial.ResponseItem(null, data, opensocial.ResponseItem.Error.BAD_REQUEST);  
  13.     callback(responseItem);  
  14.   }  
  15. };  

こんな感じ。今のところはエラーを返す。

これでよいはず。

で動かした。
で動いた。

BAD_REQUESTになった。
次はサーバとの連携。
--
と思ったけど、何しよう。
.

0 コメント: