2014-02-22 20:13:31 +11:00
|
|
|
function sub(type,handler){
|
|
|
|
checkScope.apply(this);
|
|
|
|
|
|
|
|
if(!this._events_[type])
|
|
|
|
this._events_[type]=[];
|
|
|
|
|
|
|
|
this._events_[type].push(handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
function unsub(type,handler){
|
|
|
|
checkScope.apply(this);
|
|
|
|
|
2014-03-01 23:13:06 +11:00
|
|
|
if(type=='*'){
|
|
|
|
var params=Array.prototype.slice.call(arguments,1);
|
|
|
|
for(
|
|
|
|
var keys = Object.keys(this._events_),
|
|
|
|
count = keys.length,
|
|
|
|
i=0;
|
|
|
|
i<count;
|
|
|
|
i++
|
|
|
|
){
|
|
|
|
var args=params.unshift(keys[i]);
|
|
|
|
this.off.call(args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
if(!this._events_[type])
|
|
|
|
return;
|
|
|
|
|
|
|
|
if(!handler){
|
|
|
|
delete this._events_[type];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-01 23:13:06 +11:00
|
|
|
if(this._events_[type].length<2){
|
|
|
|
delete this._events_[type];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-02-22 20:13:31 +11:00
|
|
|
for(var i=0,
|
2014-03-01 23:13:06 +11:00
|
|
|
count=this._events_[type].length;
|
2014-02-22 20:13:31 +11:00
|
|
|
i<count;
|
2014-03-01 23:13:06 +11:00
|
|
|
i++
|
|
|
|
){
|
|
|
|
if(this._events_[type][i]==handler)
|
|
|
|
this._events_[type].splice(i,1);
|
2014-02-22 20:13:31 +11:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function pub(type){
|
|
|
|
checkScope.apply(this);
|
|
|
|
|
|
|
|
if(this._events_['*'] && type!='*'){
|
|
|
|
var params=Array.prototype.slice.call(arguments);
|
|
|
|
params.unshift('*');
|
|
|
|
this.trigger.apply(this,params);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!this._events_[type])
|
|
|
|
return;
|
|
|
|
|
|
|
|
for(var i=0,
|
|
|
|
events=this._events_[type],
|
|
|
|
count=events.length,
|
|
|
|
args=Array.prototype.slice.call(arguments,1);
|
|
|
|
i<count;
|
|
|
|
i++){
|
|
|
|
events[i].apply(this, args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkScope(){
|
|
|
|
if(!this._events_)
|
|
|
|
this._events_={};
|
|
|
|
}
|
|
|
|
|
|
|
|
function init(scope){
|
|
|
|
if(!scope)
|
|
|
|
return {
|
|
|
|
on:sub,
|
|
|
|
off:unsub,
|
|
|
|
trigger:pub
|
|
|
|
};
|
|
|
|
|
|
|
|
scope.on=(
|
|
|
|
function(scope){
|
|
|
|
return function(){
|
|
|
|
sub.apply(
|
|
|
|
scope,
|
|
|
|
Array.prototype.slice.call(arguments)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)(scope);
|
|
|
|
|
|
|
|
scope.off=(
|
|
|
|
function(scope){
|
|
|
|
return function(){
|
|
|
|
unsub.apply(
|
|
|
|
scope,
|
|
|
|
Array.prototype.slice.call(arguments)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)(scope);
|
|
|
|
|
|
|
|
scope.trigger=(
|
|
|
|
function(scope){
|
|
|
|
return function(){
|
|
|
|
pub.apply(
|
|
|
|
scope,
|
|
|
|
Array.prototype.slice.call(arguments)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)(scope);
|
|
|
|
|
|
|
|
scope._events_={};
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports=init
|