Apache Shindig Website > Index > How to support features that depend on gadgets.rpc
Added by Kevin Brown, last edited by Kevin Brown on Dec 10, 2008  (view change)

Supporting any rpc feature requires that the container include the appropriate javascript. The easiest way to get this is to source it from the gadget rendering server, i.e.:

<script src="http://example.org/gadgets/js/rpc.js?c=1"></script>

Specifying c=1 minimizes the returned set of javascript files to just that necessary to satisfy container dependencies.

You must also make sure that the rpc_relay.html (found in trunk/javascript/container/rpc_relay.html) is available to the gadget. You can do this by specifying the parent parameter in your iframe url.

You must call gadgets.rpc.setAuthToken for every gadget on the page, passing the same value for the second parameter as is included in the "rpctoken" query string parameter in the gadget's iframe. If you don't include the rpctoken parameter, you don't need to make this call, but messages can be spoofed.

Additional requirements for container to gadget message passing:

You must make sure that gadgets.rpc.setRelayUrl is called for every app, so that the container code understands where rpc_relay.html can be located.

dynamic-height

dynamic-height.js
 
gadgets.rpc.register('resize_iframe', function(height) {
 var element = document.getElementById(this.f);
 if (element) {
   // Adjust IFrame height
   height = Math.max(MIN_HEIGHT, Math.min(MAX_HEIGHT, height));
   element.style.height = height + 'px';
 }
});

setprefs

setprefs.js
 
gadgets.rpc.register('set_pref', function(token, name, value) { 
  storeSomeValueForThisGadget(this.f, name, value);
});

setprefs has a legacy wart, the 'token' parameter. You can safely ignore this for now.

settitle

settitle.js
 
gadgets.rpc.register('set_title', function(title) {
 var element = document.getElementById(this.f + '_title');
 element.innerHTML = gadgets.util.escape(title);
});

requestNavigateTo

requestNavigateTo.js
 
gadgets.rpc.register('requestNavigateTo', function(view, opt_params) {
 var url = getUrlFor(view);
 if (opt_params) {
   var paramStr = gadgets.json.stringify(opt_params);
   if (paramStr) {
     url += '&appParams=' + encodeURIComponet(paramsStr);
   }
 }

 if (url && document.location.href.indexOf(url) == -1) {
   document.location.href = url;
 }
});

pubsub

To support pubsub, you'll need to include some additional code. Replace the first script with this:

<script src="http://example.org/gadgets/js/rpc:pubsub.js?c=1"></script>

pubsub.js
 
gadgets.pubsubrouter.init(function(id) {
 // return the gadget spec url corresponding to the given gadget id.
 }
);

alternatively:

pubsub-alt.js
 
gadgets.pubsubrouter.init(
 function(id) {
 // return the gadget spec url corresponding to the given gadget id.
 },
 {
   onSubscribe: function(sender, channel) {
     // return true to reject the request.
   },
   onUnsubscribe: function(sender, channel) {
     // return true to reject the request.
   },
   onPublish: function(sender, channel, message) {
     // return true to reject the request.
   }
 }
);