Refactor Basestar#defineEvent

This commit is contained in:
Andrew Stewart 2014-02-27 18:21:30 -08:00
parent 84f06afa5b
commit 520c523971
1 changed files with 15 additions and 17 deletions

View File

@ -52,19 +52,21 @@ namespace("Cylon", function() {
// //
// Returns the source // Returns the source
Basestar.prototype.defineEvent = function(opts) { Basestar.prototype.defineEvent = function(opts) {
var sendUpdate, targetEventName, var self = this;
_this = this;
targetEventName = opts.targetEventName || opts.eventName; opts.sendUpdate = opts.sendUpdate || false;
sendUpdate = opts.sendUpdate || false; opts.targetEventName = opts.targetEventName || opts.eventName;
opts.source.on(opts.eventName, function() { opts.source.on(opts.eventName, function() {
var args, _ref, _ref1; var args = arguments.length >= 1 ? [].slice.call(arguments, 0) : [];
args = 1 <= arguments.length ? [].slice.call(arguments, 0) : []; args.unshift(opts.targetEventName);
(_ref = opts.target).emit.apply(_ref, [targetEventName].concat([].slice.call(args))); opts.target.emit.apply(opts.target, args);
if (sendUpdate) {
return (_ref1 = opts.target).emit.apply(_ref1, ['update', targetEventName].concat([].slice.call(args))); if (opts.sendUpdate) {
args.unshift('update');
opts.target.emit.apply(opts.target, args);
} }
}); })
return opts.source;
}; };
// Public: Creates an event handler that proxies events from an adaptor's // Public: Creates an event handler that proxies events from an adaptor's
@ -77,9 +79,7 @@ namespace("Cylon", function() {
Basestar.prototype.defineAdaptorEvent = function(opts) { Basestar.prototype.defineAdaptorEvent = function(opts) {
opts['source'] = this.connector; opts['source'] = this.connector;
opts['target'] = this.connection; opts['target'] = this.connection;
if (opts['sendUpdate'] == null) { if (opts['sendUpdate'] == null) { opts['sendUpdate'] = false; }
opts['sendUpdate'] = false;
}
return this.defineEvent(opts); return this.defineEvent(opts);
}; };
@ -93,9 +93,7 @@ namespace("Cylon", function() {
Basestar.prototype.defineDriverEvent = function(opts) { Basestar.prototype.defineDriverEvent = function(opts) {
opts['source'] = this.connection; opts['source'] = this.connection;
opts['target'] = this.device; opts['target'] = this.device;
if (opts['sendUpdate'] == null) { if (opts['sendUpdate'] == null) { opts['sendUpdate'] = true; }
opts['sendUpdate'] = true;
}
return this.defineEvent(opts); return this.defineEvent(opts);
}; };