事件切片

QWrap群里讨论到AOP,之前也曾经讨论过方法切片,静态方法的切片在FunctionH里也曾有过,后来觉得实用场景不多而删除,另外,方法添加事件切片也讨论过,这里写一个简单的例子示意:
View Code
<HTML><HEAD><TITLE>Test</TITLE>
<META content="text/html; charset=gb2312" http-equiv=Content-Type>
<script src="http://dev.qwrap.com/resource/js/_docs/_jk/js/apps/core_dom_youa.js" type="text/javascript"></script>
</HEAD>
<body>
<div id="div1">hello1</div>
<input type=button value="change" onclick="Div1.sethtml();"/>
</body>
<script type="text/javascript">
QW.ObjectH.aopEvent
=function(obj,methodName){
var events='before,after'.split(',').map(function(el){return el+methodName.toLowerCase();});//先固定只支持这两个吧
CustEvent.createEvents(obj,events);
var method=obj[methodName];
obj[methodName]
=function(){
if(!this.fire('before'+methodName.toLowerCase())) return;
var ret=method.apply(this,arguments);
this.fire('after'+methodName.toLowerCase());
return ret;
}
};

var Div1={
sethtml:
function(){
W(
'#div1').html(Math.random()+'');
}
};
QW.ObjectH.aopEvent(Div1,
'sethtml');//aop转化
Div1.on('beforesethtml',function (e){if(!confirm('change?')) e.preventDefault();});
Div1.on(
'aftersethtml',function (){alert('值改变啦');});
</script>

</HTML>

可以看到,Div1的方法sethtml本身是没有事件机制的,经QW.ObjectH.aopEvent(Div1,'sethtml');后,就可以添加多头事件了。

事实上,关于aopEvent,我们还没想成熟,所以,在现在的QWrap中,还没有aopEvent或类似的功能。目前组件里对象方法与事件的互动,都是在方法里写死的。

posted on 2011-04-28 18:13  JKisJK  阅读(695)  评论(0编辑  收藏  举报

导航