60 lines
1.8 KiB
JavaScript
60 lines
1.8 KiB
JavaScript
// Learn cc.Class:
|
|
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html
|
|
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html
|
|
// Learn Attribute:
|
|
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
|
|
// - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html
|
|
// Learn life-cycle callbacks:
|
|
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
|
|
// - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
|
|
|
|
var clsObserver = function(target,cb){
|
|
this.target = target
|
|
this.callback = cb
|
|
}
|
|
|
|
var Notifier = function(){
|
|
this.observerMap={}
|
|
}
|
|
|
|
Notifier.prototype.on = function(name,target,callback) {
|
|
if (!this.observerMap[name]){
|
|
this.observerMap[name]=[]
|
|
}
|
|
this.observerMap[name].push(new clsObserver(target,callback))
|
|
};
|
|
Notifier.prototype.off = function(name,target) {
|
|
if (this.observerMap[name] ){
|
|
var list=this.observerMap[name]
|
|
for(var i = list.length-1;i>=0;i--){
|
|
if(list[i].target == target){
|
|
list.splice(i, 1);
|
|
break
|
|
}
|
|
}
|
|
|
|
}
|
|
};
|
|
|
|
Notifier.prototype.cleankey = function(name) {
|
|
if (this.observerMap[name] ){
|
|
this.observerMap[name].length = 0
|
|
|
|
}
|
|
};
|
|
|
|
Notifier.prototype.removeAllObservers = function(name,arr) {
|
|
this.observerMap[name] = null
|
|
};
|
|
Notifier.prototype.emit = function(name,parma) {
|
|
if (this.observerMap[name] ){
|
|
var list=this.observerMap[name]
|
|
for(var i = list.length-1;i>=0;i--){
|
|
list[i].callback(parma)
|
|
}
|
|
}
|
|
|
|
};
|
|
|
|
cc.Notifier = new Notifier();
|