zoukankan      html  css  js  c++  java
  • 事件切片

    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或类似的功能。目前组件里对象方法与事件的互动,都是在方法里写死的。
  • 相关阅读:
    运维文档的几点看法
    cmpp短信网关对接MSGID问题
    一文带解读C# 动态拦截覆盖第三方进程中的函数(外挂必备)
    C#中的解构
    如何科学破解慢SQL?
    c# HttpClient 上传文件并带参
    React 页面跳转传值
    Mysql 优化:status这类字段适合加索引吗?
    PHP数组高效去重
    PHP unset()、array_unique()的坑
  • 原文地址:https://www.cnblogs.com/jkisjk/p/aopevent_notice.html
Copyright © 2011-2022 走看看