O'Reilly logo

Programming Social Applications by Jonathan LeBlanc

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

A Practical Example

Now that we know how to initialize the cajoler within an OpenSocial gadget, let’s explore a real gadget to see how this is implemented in practice.

Our gadget’s function is to display a numeric value on the screen that defaults to 0. We will then offer a series of buttons to increment or decrement that value:

<?xml version="1.0" encoding="utf-8"?>
<Module>
   <ModulePrefs title="Caja Sample"
                title_url="http://www.jcleblanc.com"
                description="Displays a simple content section to cajole"
                author="Jonathan LeBlanc">
      <Require feature="opensocial-0.9"/>
      <Require feature="caja" />
   </ModulePrefs>
   <Content type="html">
      <![CDATA[
      <div id="number">0</div>
      <button onclick="changeNum('+')">+</button>
      <button onclick="changeNum('-')">-</button>

      <script type="text/javascript">
      //increment or decrement the counter
      function changeNum(changeType){
         var num = document.getElementById("number");
         num.innerHTML = (changeType == "+") ?
                         parseInt(num.innerHTML) + 1 :
                         parseInt(num.innerHTML) - 1;
      }
      </script>
   ]]>
   </Content>
</Module>

Our gadget includes the Caja feature that makes the JavaScript libraries for the cajoling process available in the gadget. The gadget’s Content section is what will be cajoled when the gadget is rendered in a container that supports Caja.

Once the container has cajoled our gadget’s content, the Content section ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required