Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from weekeight/dev
add document
- Loading branch information
Showing
3 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
### on(eventType, fn, [scope]) | ||
|
||
- eventType {String} 自定义事件名称.包含一个或多个事件名称的字符串, 多个事件名以空格分开。 事件可以通过加点来表示分组,例如 "click.one" , "click.two | ||
- fn {Function} 当事件触发时的回调函数 | ||
- [scope] {Object} 可选,作用域。回调函数的 this 值. 如果不指定默认为绑定事件的当前元素 | ||
|
||
为相应事件添加事件处理器 | ||
|
||
### addTarget(target) | ||
|
||
- target {Object} 事件往上冒泡的事件源 | ||
|
||
添加冒泡事件源对象 | ||
|
||
``` | ||
KISSY.use(['util', 'event-custom'], function(S, Util, CustomEvent) { | ||
function Custom(id){ | ||
this.id = id; | ||
} | ||
Util.augment(Custom, CustomEvent.Target); | ||
var c1 = new Custom("c1"); | ||
var c2 = new Custom("c1"); | ||
c1.addTarget(c2); | ||
c2.on("run",function(e){ | ||
S.log(e.target.id +" fires event run"); // => c1 fires event run | ||
}); | ||
c1.fire("run"); | ||
}); | ||
``` | ||
|
||
[添加冒泡事件源 demo](http://runjs.cn/code/bzppt8kc) | ||
### removeTarget(target) | ||
|
||
- target {Object} 事件往上冒泡的事件源 | ||
|
||
### detach(eventType, fn, [scope]) | ||
|
||
- eventType {String} 自定义事件名称 | ||
- fn {Function} 当事件触发时的回调函数 | ||
- [scope] {Object} 可选,作用域。回调函数的 this 值. 如果不指定默认为绑定事件的当前元素 | ||
|
||
解除绑定的事件处理器 | ||
|
||
### publish(eventType, cfg) | ||
|
||
- eventType {String} 自定义事件名称 | ||
|
||
- cfg {Object} 事件的具体配置对象 | ||
|
||
- [bubbles=true] {Boolean} 可选,是否支持冒泡,默认为true | ||
- [defaultFn] {Function} 默认动作函数,事件被触发时会默认执行,除非被事件对象通过preventDefault停止了 | ||
|
||
注册一个自定义事件,并配置该事件的特性,如是否能冒泡,及事件发生的默认动作函数,类似a标签的默认跳转动作 | ||
|
||
[创建自定义事件publish demo](http://runjs.cn/code/0h2ugbej) | ||
|
||
### fire(eventType, eventData) | ||
|
||
- eventType {String} 自定义事件名称 | ||
- eventData {Object} 要混入触发事件对象的数据对象 | ||
|
||
如果其中一个事件处理器返回 false , 则返回 false, 否则返回最后一个事件处理器的返回值 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
1. [添加事件冒泡事件源 addTarget](http://runjs.cn/code/bzppt8kc) | ||
2. [创建自定义事件publish](http://runjs.cn/code/0h2ugbej) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
### 使用说明 | ||
|
||
当需要使用自定义事件的时候可以使用该模块来实现,通过将 CustomEvent.Target 混入普通对象就可以使得普通对象和 DOM 节点一样也能触发,添加和删除事件了。 | ||
|
||
### 基本使用示例 | ||
|
||
``` | ||
KISSY.use(['event-custom', 'util'], function(S, CustomEvent, Util){ | ||
function Dog(name){ | ||
this.name = name; | ||
} | ||
Util.augment(Dog, CustomEvent.Target, { | ||
shout : function(){ | ||
this.fire('shout',{ | ||
content : 'I am hungry...' | ||
}); | ||
} | ||
}); | ||
var myDog = new Dog('cuteDog'); | ||
myDog.on('shout', function(ev){ | ||
alert(this.name + 'say that ' + ev.conent); | ||
}); | ||
myDog.shout(); | ||
}); | ||
``` |