感受PHPortlet技术的几个实用的特性

PHPortlet技术是CMSPAD项目的重点特性之一,在MVC架构中它起到了View与Controller通信和数据交换的功能。

大概的流程就是:客户端的脚本通过PHPortlet指定的语法对HTML页面元素进行封装,然后监听其动作,一旦有事件发生,数据会通过AJAX自动提交到服务器端相对应的Portlet组件进行处理。下面,我来说说它目前已经提供的功能:

1. 视图内容绑定(View Content Binding):在页面中通过PHP模板标签语法或纯HTML语法对HTML内容区块进行数据绑定。

用于服务器端的模板代码
[code]<{portlet name="SimplePortlet" view="simpleView"}> [/code]
用于客户端的HTML代码
[code]<div bind="SimplePortlet.simpleView"></div> [/code]
服务器端的Portlet代码
[code]<?php
class SimplePortlet extends Portlet{
public function viewSimpleView(){
return "hello world!";
}
}
?> [/code]
当然,bind的语法中还可以带有参数传递。例如:

用于服务器端的模板代码
[code]<{portlet name="SimplePortlet" view="simpleView" arg1="hello" arg2="world"}> [/code]
用于客户端的HTML代码
[code]<div bind="SimplePortlet.simpleView('hello', 'world')"></div>[/code]
服务器端的Portlet代码
[code]<?php
class SimplePortlet extends Portlet{
public function viewSimpleView($arg1, $arg2){
return "$arg1 $arg2!";
}
}
?>[/code]
当开发人员使用JavaScript脚本更改DIV元素的bind属性时,它会自动使用新的值来更新内容。

2. 超链接绑定(Hyperlink Binding):通过超链接与Portlet进行通讯。

用于客户端的HTML代码
[code]<a href="#" bind="simplePortlet.simpleLink">Hello World</a> [/code]
服务器端的Portlet代码
[code]<?php
class SimplePortlet extends Portlet{
public function linkSimpleLink($response){
return "hello world!";
}
}
?>[/code]
同样的,它也支持带参数的传递,使用方法与第1部分一样。不过,超链接绑定还支持一个重要特性,就是提交前验证功能:

用于客户端的HTML代码
[code]<script type="text/javascript">
function checkIt(){
return confirm('Pre hello world!');
}
</script>
<a href="#" bind="simplePortlet.simpleLink(): checkIt()">Hello World</a>[/code]
这样,当用户点击“Hello World"超链接时,先弹出“Pre hello world"确认对话框,当点击“是"之后才会提交,否则不会。

下面,重点技术登场了…

3. 表单绑定技术(Form Binding):对HTML的表单进行绑定,与指定的Portlet进行通讯。

用于客户端的HTML代码
[code]<form bind="SimplePortlet.simpleForm">
<input type="text" name="hello" value="world"/>
<input type="checkbox" name="check[]" value="value1"/>
<input type="checkbox" name="check[]" value="value2"/>
<button type="submit">Submit</button>
</form>[/code]
服务器端的Portlet代码
[code]<?php
class SimplePortlet extends Portlet{
public function formSimpleForm($response, $params){
$response->alert($params['hello']);
if(!empty($params['check'])){
$response->alert($params['check'][0]);
}
}
}
?>[/code]
表单绑定的bind属性同样支持提交前验证功能,语法与第2部分相同。

另外,表单还支持无刷新的文件上传功能,用户只需要增加一个form标准属性即可实现:

用于客户端的HTML代码
[code]<form bind="SimplePortlet.uploadFile" enctype="multipart/form-data">
<input type="file" name="upfile" />
<input type="hidden" name="upname" value="File Path" />
<button type="submit">Submit</button>
</form>[/code]
服务器端的Portlet代码
[code]<?php
class SimplePortlet extends Portlet{
public function formUploadFile($response, $params){
global $_FILES;
$response->alert($params['upname'].': '.$_FILES['upfile']['tmp_name']);
}
}
?> [/code]
至此,PHPortlet技术的三大特征已经说明完成。目前,此技术趋于成熟,已经有几个网站开始使用。相信以后会有更多提高开发人员效率的新特性研发出来。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值