`
yipsilon
  • 浏览: 242234 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类
最新评论

CMSPAD中新加的一个小特性:JSON服务

阅读更多
很多界面效果库都是使用JSON来提供服务的,例如ExtJS等。因此,为了更好的兼容这些类库,我们为CMSPAD增加了一个JSON服务的特性。

同样地,它也是通过Portlet来对页面进行服务的:
class SimplePortlet extends Portlet{

  // 直接返回JSON字符串
  public function jsonMyService1(){
    return '{"hello":{"world":"JSON 测试"}}';
  }

  // 返回对象时,自动转换成JSON字符串
  public function jsonMyService2(){
    return array('hello' => array('world' => 'JSON 测试'));
  }
}

而在客户端的代码中,我们可以这样调用:
<script type="text/javascript">
function callJSON(o){
  alert(o.hello.world);
}
</script>
<button type="button" onclick="cmspad.json('SimplePortlet.myService1', callJSON);">JSON 测试</button>

下面看一下在实际应用中的JSON服务:我们在经典的ExtJS Desktop 2.0演示中,可以通过远程调用来进行对桌面进行配置。源代码如下:
getDesktopConfig : function(){
  // can call server for saved module id's
  Ext.Ajax.request({
    success: function(o){
      var decoded = Ext.decode(o.responseText);
			
      if(decoded.success){
        this.initDesktopConfig(decoded.config);
      }else{
        // error
      }
    },
    failure: function(){
      // error
    },
    scope: this,
    url: 'php/DesktopConfig.php'
  });
}

上面的代码是使用Ext自带的AJAX组件进行远程调用的,而如果要使用CMSPAD的PHPortlet技术,该怎么办呢?
getDesktopConfig : function(){
  var desktop = this;
  cmspad.json('SimplePortlet.myDesktopExample', function(o){
    desktop.initDesktopConfig(o.config);
  });
}

这样,与ExtJS的整合即可完成。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics