tao-test/app/taoQtiItem/views/js/portableLib/OAT/util/EventMgr.js

46 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-08-29 20:14:13 +02:00
define(['taoQtiItem/portableLib/lodash'], function(_){
'use strict';
return function EventMgr(){
var events = {};
this.get = function get(event){
if(event && events[event]){
return _.clone(events[event]);
}else{
return [];
}
};
this.on = function on(event, callback){
var name;
var tokens = event.split('.');
if(tokens[0]){
name = tokens.shift();
events[name] = events[name] || [];
events[name].push({
ns : tokens,
callback : callback
});
}
};
this.off = function off(event){
if(event && events[event]){
events[event] = [];
}
};
this.trigger = function trigger(event, data){
if(events[event]){
_.forEach(events[event], function(e){
e.callback.apply({
type : event,
ns : []
}, data);
});
}
};
};
});