snappymail/dev/Common/Events.js

77 lines
1.2 KiB
JavaScript
Raw Normal View History

2014-08-22 23:08:56 +08:00
2014-09-05 06:49:03 +08:00
(function () {
2014-08-22 23:08:56 +08:00
2014-08-25 23:49:01 +08:00
'use strict';
2014-08-22 23:08:56 +08:00
var
2014-08-25 23:49:01 +08:00
_ = require('_'),
2014-08-25 15:10:51 +08:00
2014-09-05 06:49:03 +08:00
Utils = require('Common/Utils'),
Plugins = require('Common/Plugins')
2014-08-22 23:08:56 +08:00
;
2014-08-25 15:10:51 +08:00
2014-08-22 23:08:56 +08:00
/**
* @constructor
*/
function Events()
{
this.oSubs = {};
}
Events.prototype.oSubs = {};
/**
* @param {string} sName
* @param {Function} fFunc
* @param {Object=} oContext
* @return {Events}
*/
Events.prototype.sub = function (sName, fFunc, oContext)
{
if (Utils.isObject(sName))
2014-08-22 23:08:56 +08:00
{
oContext = fFunc || null;
fFunc = null;
_.each(sName, function (fSubFunc, sSubName) {
this.sub(sSubName, fSubFunc, oContext);
}, this);
2014-08-22 23:08:56 +08:00
}
else
{
if (Utils.isUnd(this.oSubs[sName]))
{
this.oSubs[sName] = [];
}
2014-08-22 23:08:56 +08:00
this.oSubs[sName].push([fFunc, oContext]);
}
2014-08-22 23:08:56 +08:00
return this;
};
/**
* @param {string} sName
* @param {Array=} aArgs
* @return {Events}
*/
Events.prototype.pub = function (sName, aArgs)
{
Plugins.runHook('rl-pub', [sName, aArgs]);
2014-08-25 15:10:51 +08:00
2014-08-22 23:08:56 +08:00
if (!Utils.isUnd(this.oSubs[sName]))
{
_.each(this.oSubs[sName], function (aItem) {
if (aItem[0])
{
aItem[0].apply(aItem[1] || null, aArgs || []);
}
});
}
return this;
};
module.exports = new Events();
2014-09-05 06:49:03 +08:00
}());