JavaScript API

Throughout this chapter, we have explored how to work with templates that automatically render during gadget processing. Autorendering is an excellent method for building an extensible structure for your gadget when it first loads, but it does not cover the extent of functionality that you may require when working with templates.

The templating JavaScript API seeks to address this shortcoming, allowing developers to programmatically render a template into the HTML document of a gadget through the JavaScript layer, as needed. This functionality allows the developer to enjoy the rich functionality and feature set of OpenSocial templates while still maintaining a great amount of freedom in her development practices.

Before we dive into the individual methods of the JavaScript API, note that each example assumes you have the following template defined:

<script type="text/os-template"
   <h1>Local Restaurants</h1>
      <li repeat="${restaurants}">
         <b>Name</b>: ${}<br />
         <b>Address</b>: ${Cur.address}<br />
         <b>Website</b>: <a href="${Cur.href}">

As well as the following data source for the template:

var restaurants = [ { place: "Lovely Sweets &amp; Snacks", address: "9 32 E El Camino Real, Sunnyvale CA", href: " sweets-snacks-sunnyvale" }, { place: "Vitos Famous Pizza", address: "1155 Reed Ave, Sunnyvale CA" href: " ...

Get Programming Social Applications now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.