OpenSocial Templates

来源:互联网 发布:出售淘宝买家资料数据 编辑:程序博客网 时间:2024/06/05 01:27

OpenSocial0.9提供了一种激动人心的数据动态显示的方法OpenSocial Templates。如果所有脚本技术(如PHP,JSP)一样,你可以将模板标签书写在HTML中,在页面Render的时候通过数据对象替换,而无需在JavaScript中用字符串装配你的数据内容和控制显示效果的HTML标签。
以这样一个数据对象song为例:
var song = { title: 'Love song #15',
   url: 'http://allmusic.com/johndoe/greatesthits/lovesong15.mp3',
   artist: 'John Doe',
   album: 'Greatest Hits',
   albumThumbnail: 'http://allmusic.com/johndoe/greatesthits.jpg' }
过去,你需要这样在DIV中显示这个数据对象:
document.getElementById('outputDiv').innerHTML = '<a href="' + song.url 
    + ' "><img src="' + song.albumThumbnail + '"> ' 
    + song.title + ' BY ' + song.artist + ' FROM ALBUM ' + song.album +'</a>';
现在,你只需要这样:
<script type="text/os-template"> 
  <a href="${url}"> 
    <img src="${albumThumbnail}"/>   ${title} BY ${artist} FROM ALBUM ${album}
  </a> 
</script>
清晰,可以重用的HTML标签足够让你眼前一亮吧,是的,更激动的是,你可以使用Web设计工具直接制作各种效果了,在Dreamweaver中编辑好样式,将HTML代码直接复制到<script type="text/os-template">标签中。在这个标签中定义的,就是OpenSocial Templates,你可以在其中使用基本的HTML标签,如例子中的a、img。你还可以按照JavaScript的方式,在${}中使用表达式。
要使用Opensocial Templates,你需要在gadget中引用opensocial-dataopensocial-templates模块。看下面这个完整的例子:
<?xml version="1.0" encoding="UTF-8"?> 
  <Module> 
    <ModulePrefs title="Song List"> 
      <Require feature="opensocial-data" /> 
      <Require feature="opensocial-templates"> 
    </Require> 
  </ModulePrefs> 
  <Content type="html"> <![CDATA[ 
      <script type="text/os-data" xmlns:os="http://ns.opensocial.org/2008/markup"> 
        <os:HttpRequest key="song" href="http://mysongserver.com"/>
      </script>   
      <script type="text/os-template" require="song">
        <a href="${song.url}">
          <img src="${song.albumThumbnail}"/> ${song.title} BY ${song.artist} FROM ALBUM ${song.album} 
        </a>
      </script>
    ]]> </Content> 
</Module>
在这个例子中,song对象通过HttpRequest从http://mysongserver.com处获取,然后通过os-template显示。song对象需要在os-template的标签中通过require属性引入。在模板中,便可以使用song对象的属性构成表达式。
如果我们有一个对象数组,songs:
songs : [ { title: 'Love song #15',
         url: 'http://allmusic.com/johndoe/greatesthits/lovesong15.mp3',
         artist: 'John Doe',
         album: 'Greatest Hits',
         albumThumbnail: 'http://allmusic.com/johndoe/greatesthits.jpg' },
       { title: 'Love song #16',
         url: 'http://allmusic.com/janedoe/greatesthits/lovesong16.mp3',
         artist: 'Jane Doe',
         album: 'Best Of',
         albumThumbnail: 'http://allmusic.com/janedoe/bestof.jpg' },
       { title: 'Single #1',
         url: 'http://allmusic.com/janedoe/greatesthits/single1.mp3',
         artist: 'Jane Doe',
         album: 'Greatest Hits',
         albumThumbnail: 'http://allmusic.com/janedoe/greatesthits.jpg' }, ]
我们可以使用repeat属性去循环模板标签,遍历这个数组,如下,<li>标签会被循环。
<script type="text/os-template" require="songs"> 
  <ul> 
    <li repeat="${songs}"> 
      <a href="${url}"> 
        <img src="${albumThumbnail}"/> ${title} BY ${artist} FROM ALBUM ${album} 
      </a> 
    </li> 
  </ul> 
</script>

你还可以使用条件表达式,配合循环来实现数据的筛选:
<script type="text/os-template" require="songs"> 
  <ul> 
    <li repeat="${songs}"> 
      <span if="${album == 'Greatest Hits'}"> 
        <a href="${url}"> 
        <img src="${albumThumbnail}"/> ${title} BY ${artist} FROM ALBUM ${album} 
        </a> 
      </span> 
    </li> 
  </ul> 
</script>
只有album为"Greatest Hits"的song会被显示。
写到这里你会发现,虽然服务器端的模板语言已经再熟悉不过了,但在JavaScript中使用模板技术却是个新鲜而又快乐的事情。看上去这和OpenSocial无关,那么看看下面的例子,通过Template来显示OpenSocial容器中的对象。
<?xml version="1.0" encoding="UTF-8"?> 
<Module> 
  <ModulePrefs title="Server-side Template">
    <Require feature="opensocial-data" /> 
    <Require feature="opensocial-templates" /> 
</ModulePrefs> 
  <Content type="html"> <![CDATA[ 
    <script xmlns:os="http://ns.opensocial.org/2008/markup" type="text/os-data">
      <os:PeopleRequest key="friends" userId="@viewer" groupId="@friends"/>
    </script>
    <script type="text/os-template">
        <ul>
          <li repeat="${friends}">
            <span id="id${Context.Index}">${Cur.name.givenName}</span>
          </li>
        </ul>
     </script>
    ]]> </Content> 
</Module>
获取访问者的好友列表,并显示出来,想想原来冗长的JavaScript代码,而且夹杂着数据对象和HTML标签的字符串拼接,以上清晰的书写方式的确激动人心。
无需我再多说,你是不是已经跃跃欲试,要用OpenSocial Templates来重写你的OpenSocial应用?不过遗憾的告诉你,目前还没有容器支持OpenSocial0.9,Apache Shindig支持OpenSocial0.9的版本(v1.1)也还没有Release。好了,就当我们先学习一下吧。
(原文参见:http://wiki.opensocial.org/index.php?title=OpenSocial_Templates)

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 市民卡没有磁性怎么办 手机拍视频反光怎么办 摄影师接不到单子怎么办 淘宝七天没发货怎么办 淘宝商家漏发货怎么办 毒app虚假发货怎么办 淘宝店拒绝发货怎么办 买东西不给发票怎么办 淘宝店不想发货怎么办 淘宝商品不发货怎么办 买家恶意不退款怎么办 手机淘宝不发货怎么办 闲鱼虚假发货怎么办 买假货没发票怎么办 淘宝发票丢了怎么办 淘宝卖家不给发票怎么办 淘宝如果不要发票怎么办 闲鱼没有发票怎么办 淘宝店没有发票怎么办 物流不提供发票怎么办 电子发票重复报销怎么办 鬼火摩托车没电怎么办 摩托粘了缸怎么办 摩托淹缸了怎么办? 晚上睡不着早上睡不醒怎么办 三唑仑吃了以后难受怎么办 吃两片三唑仑第二天头晕怎么办 买家收货不付钱怎么办 淘宝上买东西被骗怎么办 被淘宝客服诈骗怎么办 淘宝开店被骗了怎么办 做淘宝被骗了怎么办 淘宝买证被骗怎么办 淘宝买狗被骗怎么办 微店扣分了怎么办 微店不同意退款怎么办 微店商品被下架怎么办 淘宝不支持花呗怎么办 淘宝误点了收货怎么办 注销公司社保户怎么办 手机媒体声音小怎么办