-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
crossjs
committed
Sep 29, 2014
1 parent
f641d68
commit 25ff77d
Showing
3 changed files
with
167 additions
and
161 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 |
---|---|---|
|
@@ -30,6 +30,6 @@ | |
"strict": false, | ||
"sub": true, | ||
"undef": true, | ||
"unused": false, | ||
"unused": "vars", | ||
"multistr": 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 |
---|---|---|
|
@@ -47,7 +47,6 @@ | |
"spm": { | ||
"output": [ "events.js" ], | ||
"alias": { | ||
"$": "$" | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,198 +1,205 @@ | ||
define(function (require, exports, module) { | ||
|
||
/** | ||
* 事件 | ||
* @module Events | ||
*/ | ||
|
||
'use strict'; | ||
|
||
var $ = require('$'); | ||
|
||
var EVENT_SPLITTER = /\s+/; | ||
|
||
/** | ||
* 事件类 | ||
* 实现了事件订阅与发布 | ||
* @example | ||
* ``` | ||
* var events = new Events(); | ||
* ``` | ||
* @class Events | ||
* @constructor | ||
*/ | ||
var Events = function () { | ||
}; | ||
|
||
Events.prototype = { | ||
define(function(require, exports, module) { | ||
|
||
/** | ||
* 绑定事件订阅 | ||
* @example | ||
* ``` | ||
* events.on('test', function (e, a) { | ||
* // e.type === 'test' | ||
* }); | ||
* events.on('test test2', function (e, a) { | ||
* // e.type === 'test' or 'test2' | ||
* }); | ||
* var testFunc = function (e, a) { | ||
* // e.type === 'test' | ||
* }; | ||
* events.on({ | ||
* test: testFunc | ||
* }); | ||
* ``` | ||
* @method on | ||
* @param {String|Object} event 事件名或哈希表 | ||
* @param {Function} [callback] 回调函数 | ||
* @return {Object} 当前实例 | ||
* 事件 | ||
* @module Events | ||
*/ | ||
on: function (event, callback) { | ||
var cache = this.__events || (this.__events = {}), | ||
maps = {}, | ||
list; | ||
|
||
if ($.isPlainObject(event)) { | ||
maps = event; | ||
} else { | ||
maps[event] = callback; | ||
} | ||
|
||
$.each(maps, function (events, callback) { | ||
events = events.split(EVENT_SPLITTER); | ||
while ((event = events.shift())) { | ||
list = cache[event] || (cache[event] = []); | ||
list.push(callback); | ||
} | ||
}); | ||
'use strict'; | ||
|
||
return this; | ||
}, | ||
var EVENT_SPLITTER = /\s+/; | ||
|
||
/** | ||
* 仅执行一次的事件订阅 | ||
* @method once | ||
* @param {String} event 事件名 | ||
* @param {Function} [callback] 回调函数 | ||
* @return {Object} 当前实例 | ||
*/ | ||
once: function (event, callback) { | ||
var cb = function () { | ||
this.off(event, cb); | ||
callback.apply(this, arguments); | ||
}; | ||
return this.on(event, cb); | ||
}, | ||
function isObj(obj) { | ||
return !!(obj && | ||
Object.prototype.toString.call(obj) === '[object Object]' && | ||
obj.constructor && | ||
obj.constructor.prototype.hasOwnProperty && | ||
obj.constructor.prototype.hasOwnProperty('isPrototypeOf')); | ||
} | ||
|
||
/** | ||
* 解除事件订阅 | ||
* 事件类 | ||
* 实现了事件订阅与发布 | ||
* @example | ||
* ``` | ||
* events.off('test'); | ||
* events.off('test test2'); | ||
* events.off('test', testFunc); | ||
* var events = new Events(); | ||
* ``` | ||
* @method off | ||
* @param {String} event 事件名 | ||
* @param {Function} [callback] 回调函数 | ||
* @return {Object} 当前实例 | ||
* @class Events | ||
* @constructor | ||
*/ | ||
off: function (event, callback) { | ||
var cache = this.__events, | ||
events, | ||
list, i; | ||
var Events = function() {}; | ||
|
||
Events.prototype = { | ||
|
||
/** | ||
* 绑定事件订阅 | ||
* @example | ||
* ``` | ||
* events.on('test', function (e, a) { | ||
* // e.type === 'test' | ||
* }); | ||
* events.on('test test2', function (e, a) { | ||
* // e.type === 'test' or 'test2' | ||
* }); | ||
* var testFunc = function (e, a) { | ||
* // e.type === 'test' | ||
* }; | ||
* events.on({ | ||
* test: testFunc | ||
* }); | ||
* ``` | ||
* @method on | ||
* @param {String|Object} event 事件名或哈希表 | ||
* @param {Function} [callback] 回调函数 | ||
* @return {Object} 当前实例 | ||
*/ | ||
on: function(event, callback) { | ||
var cache = this.__events || (this.__events = {}), | ||
maps = {}, | ||
p, | ||
events, | ||
list; | ||
|
||
if (isObj(event)) { | ||
maps = event; | ||
} else { | ||
maps[event] = callback; | ||
} | ||
|
||
if (!cache) { | ||
return this; | ||
} | ||
for (p in maps) { | ||
events = p.split(EVENT_SPLITTER); | ||
while ((event = events.shift())) { | ||
list = cache[event] || (cache[event] = []); | ||
list.push(maps[p]); | ||
} | ||
} | ||
|
||
if (!event) { | ||
this.__events = {}; | ||
return this; | ||
} | ||
}, | ||
|
||
/** | ||
* 仅执行一次的事件订阅 | ||
* @method once | ||
* @param {String} event 事件名 | ||
* @param {Function} [callback] 回调函数 | ||
* @return {Object} 当前实例 | ||
*/ | ||
once: function(event, callback) { | ||
var cb = function() { | ||
this.off(event, cb); | ||
callback.apply(this, arguments); | ||
}; | ||
return this.on(event, cb); | ||
}, | ||
|
||
/** | ||
* 解除事件订阅 | ||
* @example | ||
* ``` | ||
* events.off('test'); | ||
* events.off('test test2'); | ||
* events.off('test', testFunc); | ||
* ``` | ||
* @method off | ||
* @param {String} event 事件名 | ||
* @param {Function} [callback] 回调函数 | ||
* @return {Object} 当前实例 | ||
*/ | ||
off: function(event, callback) { | ||
var cache = this.__events, | ||
events, | ||
list, i; | ||
|
||
if (!cache) { | ||
return this; | ||
} | ||
|
||
if (!event) { | ||
this.__events = {}; | ||
return this; | ||
} | ||
|
||
events = event.split(EVENT_SPLITTER); | ||
events = event.split(EVENT_SPLITTER); | ||
|
||
while ((event = events.shift())) { | ||
list = cache[event]; | ||
while ((event = events.shift())) { | ||
list = cache[event]; | ||
|
||
if (!list) { | ||
continue; | ||
} | ||
if (!list) { | ||
continue; | ||
} | ||
|
||
if (!callback) { | ||
delete cache[event]; | ||
} | ||
if (!callback) { | ||
delete cache[event]; | ||
} | ||
|
||
for (i = list.length; i >= 0; i--) { | ||
if (list[i] === callback) { | ||
list.splice(i, 1); | ||
for (i = list.length; i >= 0; i--) { | ||
if (list[i] === callback) { | ||
list.splice(i, 1); | ||
} | ||
} | ||
} | ||
} | ||
|
||
return this; | ||
}, | ||
return this; | ||
}, | ||
|
||
/** | ||
* 触发绑定的事件,有别名:`emit` 与 `trigger` | ||
* @example | ||
* ``` | ||
* events.fire('test', 'blah'); | ||
* events.fire('test2', 'blah'); | ||
* events.fire('test test2', 'blah'); | ||
* ``` | ||
* @method fire | ||
* @param {String} event 事件名 | ||
* @return {Boolean} 如有事件函数返回值 `false`,则此方法返回值为 `false` | ||
*/ | ||
fire: function(event) { | ||
var cache = this.__events, | ||
args, events, newArgs, target, | ||
list, i, n, | ||
returned = true; | ||
|
||
if (!(cache && event)) { | ||
return returned; | ||
} | ||
|
||
/** | ||
* 触发绑定的事件,有别名:`emit` 与 `trigger` | ||
* @example | ||
* ``` | ||
* events.fire('test', 'blah'); | ||
* events.fire('test2', 'blah'); | ||
* events.fire('test test2', 'blah'); | ||
* ``` | ||
* @method fire | ||
* @param {String} event 事件名 | ||
* @return {Boolean} 如有事件函数返回值 `false`,则此方法返回值为 `false` | ||
*/ | ||
fire: function (event) { | ||
var cache = this.__events, | ||
args, events, newArgs, target, | ||
list, i, n, | ||
returned = true; | ||
args = Array.prototype.slice.call(arguments, 1); | ||
|
||
if (!(cache && event)) { | ||
return returned; | ||
} | ||
target = event.target || this; | ||
events = (event.type || event).split(EVENT_SPLITTER); | ||
|
||
args = Array.prototype.slice.call(arguments, 1); | ||
while ((event = events.shift())) { | ||
|
||
target = event.target || this; | ||
events = (event.type || event).split(EVENT_SPLITTER); | ||
newArgs = [{ | ||
type: event, | ||
target: target | ||
}].concat(args); | ||
|
||
while ((event = events.shift())) { | ||
if (event !== 'all') { | ||
list = cache[event]; | ||
} | ||
|
||
newArgs = [{ | ||
type: event, | ||
target: target | ||
}].concat(args); | ||
if (!list) { | ||
list = []; | ||
} | ||
|
||
if (event !== 'all') { | ||
list = cache[event]; | ||
} | ||
// 加入 all | ||
list = list.concat(cache.all || []); | ||
|
||
if (!list) { | ||
list = []; | ||
for (i = 0, n = list.length; i < n; i++) { | ||
(list[i].apply(this, newArgs) === false) && (returned = false); | ||
} | ||
} | ||
|
||
// 加入 all | ||
list = list.concat(cache.all || []); | ||
|
||
for (i = 0, n = list.length; i < n; i++) { | ||
(list[i].apply(this, newArgs) === false) && (returned = false); | ||
} | ||
return returned; | ||
} | ||
|
||
return returned; | ||
} | ||
|
||
}; | ||
}; | ||
|
||
// alias | ||
Events.prototype.emit = Events.prototype.trigger = Events.prototype.fire; | ||
// alias | ||
Events.prototype.emit = Events.prototype.trigger = Events.prototype.fire; | ||
|
||
module.exports = Events; | ||
module.exports = Events; | ||
|
||
}); |