YUI团队在种种场合不断的夸耀自己的事件体系是多么强大:

  • YUI 3′s Event module is one of the strengths of the library –Eric Miraglia, YUI Theater — Luke Smith: “Events Evolved”
  • YUI 3 is not all about DOM manipulation — it also contains a robust set of class/object management tools, not to mention our powerful custom events –Tilo Mitra, 10 Things I Learned While Interning at YUI
  • One of the strengths of the YUI App Framework is that it’s integrated tightly with the rest of YUI and benefits from YUI’s fantastic event system and plugin/extension infrastructure. –Ryan Grove, How can I decided whether to choose YUI 3’s MVC or Backbone for a new project?

事实的确如此吗?就使用YUI的开发者反馈来看,应该是不错的:

  • AFAIK YUI 3’s event system is the most sophisticated of any JavaScript framework. Am I wrong in thinking that? –Walter Rumsby
  • I love the event system in YUI. Pure awesomeness. –Kevin Isom
  • I am constantly impressed by the degree of excellence I find in working with the YUI3 framework –Andrew Wooldridge, Cross YUI Communication and Custom Events

作为一名YUI用户,我对其事件体系的强大深有体会。从本篇文章起,我将对YUI事件机制做一个全面分析。

本次我们介绍的是比较基础的两个对象Y.EventHandleY.Do。千里之行积于跬步,YUI整套事件机制也是从这两个对象开始构筑的。

Y.EventHandle

Y.EventHandle的作用很简单:注销事件/消息监听。

Y.EventHandle = function (evt, sub) {this.evt = evt; // 事件对象this.sub = sub; // 监听对象
};
Y.EventHandle.detach = function () {this.evt._delete(this.sub); // 执行event对象的_delete方法,注销事件/消息监听return true;
};

Y.Do

Y.Do的作用是:向对象方法前面或者后面插入其它方法(前置、后置方法),以达到动态修改对象行为的目的。这种方式,也称作AOP。

示例

让我们先来看个简单的例子:

// 例1
YUI().use('event-custom', function (Y) {var cat = {eat: function () {console.log('eat a fish');}};cat.eat(); // output: eat a fishvar beforeHandle = Y.Do.before(function () {console.log('catch a fish');}, cat, 'eat');var afterHandle = Y.Do.after(function () {console.log('done!');}, cat, 'eat');cat.eat(); // output: catch a fish, eat, done!afterHandle.detach();cat.eat(); // output: catch a fish, eat
});

在不修改原对象方法的基础上,可以方便的添加前置、后置方法,并且注销这些方法也很容易。Y.Do非常漂亮的解决了我们动态修改对象方法的需求!很难想象,如果不用Y.Do代码会复杂成怎样。

源代码分析

接下来,让我们看看YUI的内部实现吧。这是多么有趣的事,就像小时候买把手枪,想不明白为什么可以射击,就砸开一看究竟。

为了更容易的看懂代码的核心,我做了适当的简化,感兴趣的朋友可以去看未删节的源码。

// 代码版本为YUI3.4.1,YUI3.5.0对Y.Do的实现有所改进
var DO_BEFORE = 0,DO_AFTER = 1;
Y.Do = {// 缓存处理对象objs: {},before: function (fn, obj, sFn) {return this._inject(DO_BEFORE, fn, obj, sFn);},after: function (fn, obj, sFn) {return this._inject(DO_AFTER, fn, obj, sFn);},_inject: function (when, fn, obj, sFn) {var id = Y.stamp(obj), o, sid;if (!this.objs[id]) this.objs[id] = {};o = this.objs[id];if (!o[sFn]) {// 创建保存对象、方法名的Method对象o[sFn] = new Y.Do.Method(obj, sFn);// 修改对象方法obj[sFn] = function () {return o[sFn].exec.apply(o[sFn], arguments);};}sid = id + Y.stamp(fn) + sFn;// 注册插入方法o[sFn].register(sid, fn, when);// 返回EventHandle对象,方便注销return new Y.EventHandle(o[sFn], sid);}
}Y.Do.Method = function (obj, sFn) {this.obj = obj;this.methodName = sFn;this.method = obj[sFn];this.before = {};this.after = {};
};
Y.Do.Method.prototype.register = function (sid, fn, when) {if (when) {this.after[sid] = fn;} else {this.before[sid] = fn;}
};
// 注销插入方法
Y.Do.Method.prototype._delete = function (sid) {delete this.before[sid];delete this.after[sid];
};
Y.Do.Method.prototype.exec = function () {var before = this.before,after = this.after,i, ret;// 执行插入前面的方法for (i in before) {if (before.hasOwnProperty(i)) {ret = before[i].apply(this.obj, arguments);}}// 执行原方法ret = this.method.apply(this.obj, arguments);// 执行插入后面的方法for (i in after) {if (after.hasOwnProperty(i)) {ret = after[i].apply(this.obj, arguments);}}return ret;
};

适用场景

a) 动态修改对象方法

请参照例1。

b) 动态修改原型方法

原型也是对象,所以,另外一个适用场景就是修改原型方法。

// 例2
YUI().use('event-custom', function (Y) {function Car(brand) {this.brand = brand;};Car.prototype.start = function () {console.log('start');};var myCar = new Car('bmw');Y.Do.before(function () {console.log('open the door');}, Car.prototype, 'start');Y.Do.after(function () {console.log('the car is started!');}, Car.prototype, 'start');myCar.start(); // output: open the door, start, the car is started!
});

c) 动态修改宿主方法

为宿主对象添加插件时,插件往往需要在宿主一些方法前后执行某些操作。YUI提供了一个很好的例子。

d) 动态修改被扩展对象方法

为对象添加扩展时,扩展对象往往需要在被扩展对象一些方法前后执行某些操作。YUI提供了一个很好的例子。

进阶使用

由于简化代码,省略了一些细节。Y.Do还有很多功能,例如:可以根据前置方法返回值阻止默认方法执行、替换参数等等。下面介绍一些这样的进阶使用方式:

// 例3
YUI().use('event-custom', function (Y) {function Car(brand, degree) {this.brand = brand;this.degree = degree || 0;};Car.prototype.shift = function (degree) {console.log('change to ' + degree);};var myCar = new Car('bmw');// 多个前置方法Y.Do.before(function (degree) {console.log('prepare to change');}, Car.prototype, 'shift');Y.Do.before(function (degree) {console.log('prepare to change again');}, Car.prototype, 'shift');myCar.shift(1); // output: prepare to change, prepare to change again, change to 1// 多个后置方法Y.Do.after(function (degree) {console.log('already change');}, Car.prototype, 'shift');Y.Do.after(function (degree) {console.log('already change again');}, Car.prototype, 'shift');myCar.shift(2); // output: ..., change to 2, already change, already change again // 中止执行Y.Do.before(function (degree) {if (degree < 0) {console.log('halt, too low!');return new Y.Do.Halt();}}, Car.prototype, 'shift');myCar.shift(-1); // output: ..., halt, too low! // 阻止默认方法Y.Do.before(function (degree) {if (degree > 4) {console.log('prevent changing, too high!');return new Y.Do.Prevent();}}, Car.prototype, 'shift');myCar.shift(5); // output: ..., prevent changing, too high!, already change, ... // 替换参数Y.Do.before(function (degree) {var d = Math.floor(degree);if (degree !== d) {return new Y.Do.AlterArgs('degree should be a integer', [d]);}}, Car.prototype, 'shift');myCar.shift(2.5); // output: ..., change to 2, ... // 替换返回值Y.Do.after(function (degree) {if (degree === 0) {return new Y.Do.AlterReturn('', 'wow, your car now has no power');}}, Car.prototype, 'shift');var ret = myCar.shift(0); // output: ..., change to 0, ... console.log(ret); // wow, your car now has no power
});

参考

  • YUILibrary-Do
  • YUILibrary-EventTarget
  • Wikipedia-AOP

YUI事件体系之Y.Do相关推荐

  1. YUI事件体系之Y.EventTarget

    上两篇文章YUI事件体系之Y.Do.YUI事件体系之Y.CustomEvent中,分别介绍了YUI实现AOP的Y.Do对象,以及建立自定义事件机制的Y.CustomEvent对象. 本篇文章,将要介绍 ...

  2. YUI事件体系之Y.CustomEvent

    上一篇文章中,简要介绍了YUI实现AOP的Y.Do对象. 接下来,我们继续对YUI事件体系进行探索.本次要介绍的是Y.CustomEvent对象,从命名上就可以看出,这个对象在整个YUI事件体系中十分 ...

  3. 《Android开发艺术探索》读书笔记 (3) 第3章 View的事件体系

    本节和<Android群英传>中的第五章Scroll分析有关系,建议先阅读该章的总结 第3章 View的事件体系 3.1 View基本知识 (1)view的层次结构:ViewGroup也是 ...

  4. View的事件体系之三 android事件分发机制详解(下)

    接着上一篇来分析事件分发机制,在看了各位大牛的关于事件分发机制的分析后茅塞顿开,之前看过好几遍郭霖,弘扬以及玉刚大神关于事件体系的讲解,一直看不懂,比较模糊,最近复习时,看到一篇博文,写的相当精彩,看 ...

  5. View的事件体系(上)(View基础知识,滑动,弹性滑动)

    View不是四大组件之一,但重要性堪比四大组件,本篇博文主要讲解View的事件体系,包括View的基础知识,滑动,弹性滑动,事件分发机制,滑动冲突的种类与解决方案. 一 View的基础知识 (1).V ...

  6. android点击事件的优先级,Android事件体系全面总结+实践分析,系列篇

    前言 在这一个月里,我利用闲余的时间看了下最近Android职业发展这块该怎么选择?这个问题各位大神的回答都非常透彻,相信对大家或多或少都在一定程度上有很大的帮助,今天在这里写这篇文章更多的是想以我开 ...

  7. android知识回顾--view的事件体系

    1.view的滑动,六种滑动方式:    一:通过layout来实现滑动效果      package com.example.testdragview; import android.content ...

  8. View的事件体系之二 View的滑动以及弹性滑动

    新年第一更,之前也有看过View体系系列文章,内容有点生疏了,重新温习一下,基础篇已经整理过了,接下来会重新梳理一遍关于View的整个体系的知识,权当复习了. 在Android设备上,滑动几乎是应用的 ...

  9. 第3章 View的事件体系

    一.View基础 View的位置参数: 参数:top,left,right,bottom x,y,translationX,translationY(android3.0之后) 四个顶点确定:top( ...

最新文章

  1. 【OF框架】使用OF.WinService项目,添加定时服务,进行创建启动停止删除服务操作...
  2. 推荐系统笔记:基于SVD的协同过滤
  3. Ecplise中配置Tomcat7服务器
  4. Memcached 1.5.13 发布,支持 TLS
  5. 框架如何调用存储过程_如何在FastReport.Net中使用存储过程
  6. Unity AssetBundles and Resources指引 (三) AssetBundle基础
  7. 变量 常量 作用域和命名规范
  8. Outlook式样界面菜单和页面控制
  9. Jenkins环境拓扑及部署流程
  10. [leetcode]746. 使用最小花费爬楼梯
  11. A*解决传教士与野人问题
  12. c语言合并两个顺序表算法,顺序表的两种合并操作(C语言)
  13. Uber牵手戴姆勒共事自动驾驶,探索未来出行模式
  14. Android多媒体框架(二)Codec初始化及Omx组件创建
  15. 第二十九篇:故障处理流程
  16. three.js textureLoader加载图片失败
  17. 组播路由协议基础——PIM-SM BSR工作机制
  18. Java影院网上购票管理系统_java影院售票管理系统
  19. 免费CDN和网站速度测试工具
  20. 从DBA职业转换看技能更迭软实力的提升

热门文章

  1. Linux shell脚本 遍历带空格的文件名
  2. 方向盘开极品飞车9很Hapyy
  3. 在window下搭建TensorFlow
  4. boost::function的用法(二)
  5. 一文读懂 | 进程并发与同步
  6. 一位跟我一样疑惑的同学~
  7. 拨号云服务器怎么自动配置网关_云服务器配置网站卡慢怎么办
  8. JS高级——深入剖析函数中的this指向问题
  9. LeetCode 1824. 最少侧跳次数(DP)
  10. LeetCode 1771. 由子序列构造的最长回文串的长度(最长回文子序)