diff --git a/lib/connection.js b/lib/connection.js index 938bab1..abc2082 100644 --- a/lib/connection.js +++ b/lib/connection.js @@ -63,12 +63,7 @@ Connection.prototype.connect = function(callback) { var msg = this._logstring("Connecting to"); Logger.info(msg); this.adaptor.connect(function() { - for (var opt in this.adaptor) { - if (!this[opt] && typeof this.adaptor[opt] === 'function') { - this[opt] = this.adaptor[opt].bind(this.adaptor); - } - } - + Utils.proxyFunctions(this.adaptor, this) callback.apply(this, arguments); }.bind(this)); }; diff --git a/lib/device.js b/lib/device.js index 18a7d08..6abce55 100644 --- a/lib/device.js +++ b/lib/device.js @@ -64,12 +64,7 @@ Device.prototype.start = function(callback) { Logger.info(msg); this.driver.start(function() { - for (var opt in this.driver) { - if (!this[opt] && typeof this.driver[opt] === 'function') { - this[opt] = this.driver[opt].bind(this.driver); - } - } - + Utils.proxyFunctions(this.driver, this) callback.apply(this, arguments); }.bind(this)); }; @@ -115,7 +110,7 @@ Device.prototype.defaultConnection = function() { for (var c in this.robot.connections) { var connection = this.robot.connections[c]; - first || (first = connection); + first = first || connection; } return first; diff --git a/lib/utils.js b/lib/utils.js index c9ff4b0..9b5942d 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -100,6 +100,14 @@ var Utils = module.exports = { return child; }, + proxyFunctions: function proxyFunctions(source, target) { + for (var opt in source) { + if (!target[opt] && typeof source[opt] === 'function') { + target[opt] = source[opt].bind(source); + } + } + }, + // Public: Proxies a list of methods from one object to another. It will not // overwrite existing methods unless told to. //