Remove `self` references in favor of #bind

This commit is contained in:
Andrew Stewart 2014-07-03 09:35:24 -07:00
parent 5ce0d37640
commit f0d8b0360b
7 changed files with 18 additions and 37 deletions

View File

@ -29,7 +29,6 @@ module.exports = Adaptor = function Adaptor(opts) {
opts = {};
}
this.self = this;
this.name = opts.name;
this.connection = opts.connection;
};

View File

@ -17,8 +17,6 @@ var express = require('express'),
var Logger = require('./logger');
var API = module.exports = function API(opts) {
var self = this;
if (opts == null) {
opts = {};
}
@ -31,17 +29,17 @@ var API = module.exports = function API(opts) {
this.express.set('title', 'Cylon API Server');
this.express.use(self.setupAuth());
this.express.use(this.setupAuth());
this.express.use(bodyParser());
this.express.use(express["static"](__dirname + "/../node_modules/robeaux/"));
// set CORS headers for API requests
this.express.use(function(req, res, next) {
res.set("Access-Control-Allow-Origin", self.CORS || "*");
res.set("Access-Control-Allow-Origin", this.CORS || "*");
res.set("Access-Control-Allow-Headers", "Content-Type");
res.set('Content-Type', 'application/json');
return next();
});
}.bind(this));
// extracts command params from request
this.express.use(function(req, res, next) {
@ -113,13 +111,11 @@ API.prototype.setupAuth = function setupAuth() {
};
API.prototype.listen = function() {
var self = this;
this.server.listen(this.port, this.host, null, function() {
var title = self.express.get('title');
var protocol = self.ssl ? "https" : "http";
var title = this.express.get('title');
var protocol = this.ssl ? "https" : "http";
Logger.info(title + " is now online.");
Logger.info("Listening at " + protocol + "://" + self.host + ":" + self.port);
});
Logger.info("Listening at " + protocol + "://" + this.host + ":" + this.port);
}.bind(this));
};

View File

@ -39,14 +39,13 @@ module.exports = Connection = function Connection(opts) {
this.connect = this.connect.bind(this);
this.self = this;
this.robot = opts.robot;
this.name = opts.name;
this.connection_id = opts.id;
this.port = opts.port;
this.adaptor = this.initAdaptor(opts);
Utils.proxyFunctionsToObject(this.adaptor.commands, this.adaptor, this.self);
Utils.proxyFunctionsToObject(this.adaptor.commands, this.adaptor, this);
};
Utils.subclass(Connection, EventEmitter);
@ -93,7 +92,7 @@ Connection.prototype.disconnect = function(callback) {
// Returns the set-up adaptor
Connection.prototype.initAdaptor = function(opts) {
Logger.debug("Loading adaptor '" + opts.adaptor + "'.");
return this.robot.initAdaptor(opts.adaptor, this.self, opts);
return this.robot.initAdaptor(opts.adaptor, this, opts);
};
// Public: Halt the adaptor's connection

View File

@ -53,8 +53,6 @@ var Robot = module.exports = function Robot(opts) {
opts = {};
}
var self = this;
var methods = [
"toString",
"registerDriver",
@ -70,8 +68,8 @@ var Robot = module.exports = function Robot(opts) {
];
methods.forEach(function(method) {
self[method] = self[method].bind(self);
});
this[method] = this[method].bind(this);
}.bind(this));
this.name = opts.name || this.constructor.randomName();
this.connections = {};
@ -209,21 +207,19 @@ Robot.prototype._createDevice = function(device) {
//
// Returns the result of the work
Robot.prototype.start = function() {
var self = this;
var begin = function(callback) {
self.work.call(self, self);
self.running = true;
self.emit('working');
this.work.call(this, this);
this.running = true;
this.emit('working');
Logger.info('Working.');
callback(null, true);
};
}.bind(this);
Async.series([
self.startConnections,
self.startDevices,
this.startConnections,
this.startDevices,
begin
], function(err) {
if (!!err) {

View File

@ -11,10 +11,6 @@ describe("Adaptor", function() {
var adaptor = new Adaptor({ name: 'adaptor', connection: connection });
describe("#constructor", function() {
it("sets @self as a reference to the adaptor", function() {
expect(adaptor.self).to.be.eql(adaptor);
});
it("sets @name to the provided name", function() {
expect(adaptor.name).to.be.eql('adaptor');
});

View File

@ -13,10 +13,6 @@ describe("Connection", function() {
var connection = robot.connections.loopback;
describe("#constructor", function() {
it("sets @self as a circular reference", function() {
expect(connection.self).to.be.eql(connection);
});
it("sets @robot to the passed robot", function() {
expect(connection.robot).to.be.eql(robot);
});

View File

@ -164,9 +164,8 @@ describe("Utils", function() {
var TestClass = (function() {
function TestClass() {
this.self = this;
this.testInstance = new ProxyClass;
utils.proxyFunctionsToObject(methods, this.testInstance, this.self, true);
utils.proxyFunctionsToObject(methods, this.testInstance, this, true);
}
return TestClass;