node.js - Javascript - How to save a reference to "this" for access in named function callback using prototype pattern -


I have problems with reference to a JavaScript object implemented with prototype pattern within the callback. I callback from a third party component That's what I use within my object. Third party object connects to a message bus.

The following pseudo code shows how I started (the actual code is working for this)

  var mb = require ('MsgBus') TestClass = function () {This.messagebus = new mb.MsgBus (); This.messagebus.connect (function (err) {if (error) console.log ("Error connecting"); else console.log ("connected");}); }   

But the callback reports an error, but then I wanted to try to add it automatically again. If I say "if.messagebus.connection" then if I have to add another anonymously method to block (if), then I can not even enter one more line, and it only has to run on it, so, I I want to divide callback logic into a named function like this

  var mb = require ('MsgBus') TestClass = function () {this.messagebus = new mb.msgBus (); This.messagebus.connect (msgBusConnectCallback); } Function msgbusConnectClallback (error) {if (error) it ???. Messagebus.connect (msgBusConnectCallback); Else console.log ("connected"); }); }   

The callback function is called, but I can not understand how to reconnect the context of the object call. I tried to make a prototype function of the object in the callback, but there is no reference. I can not create a variable in the global scope to maintain "this" because users of this class can create multiple instances of the class . I'm quite new in javascript, so I do not know if I'm missing something or need to take me completely differently? I appreciate any help and / or direction.

  this.messagebus.connect.apply (this, [msgBusConnectCallback]);    

Comments

Popular posts from this blog

php - PDO bindParam() fatal error -

logging - How can I log both the Request.InputStream and Response.OutputStream traffic in my ASP.NET MVC3 Application for specific Actions? -

java - Why my included JSP file won't get processed correctly? -