I have an object for each room I’m joining with Strophe. This object contains a function for handling presence stanzas for this particular room.
function Room(name, someData)
this.name = name;
this.someData = someData;
this.presenceHandler = function(presence) {
console.log(this.name, this.someData);
}
this.join = function() {
connection.addHandler(this.presenceHandler,null,"presence",null,null,this.name);
connection.send(/*presence*/);
}
}
var connection = new Strophe.Connection(/*http-bind*/);
var mainRoom = new Room("main", {foo: "bar"});
mainRoom.join();
But when the mainRoom.presenceHandler() function is called by an stanza by Strophe, this in the function refers to the stanza itself and not to mainRoom anymore, so I cannot access the attributes from mainRoom.
Could you tell me, how I can access the attributes of the room object from within the presenceHandler function?
Replace above code with this
note the closures for the handler