I'm having some trouble getting this.something to reference what I want it to reference. I've read through this to help me:
quirksmode.org/js/this.html
My problem is that when I try to call this.runtime.trigger in certain locations in my code, it tells me that it can't call method trigger of undefined. So this.runtime obviously isn't pointing where I want it to point.
Here's what I have:
instanceProto.onCreate initiates a websocket. When that socket receives a message, I want it to store data from the message in an object that my conditions and expressions will be able to see. Originally I had this:
instanceProto.onCreate = function() {
//Other stuff
var socket = new WebSocket(//My server's address
//socket stuff
socket.onmessage = function(evt) {
this.data = evt.data;
}
}
That wouldn't work, presumably because the 'this.' in 'this.data' refers to 'socket'. In order for my expressions to be able to reference the data as this.data, this would need to refer to... instanceProto? Say I want the following operation to work:
cnds.thisIsACondition = function(indexOfWantedData) {
if(this.data[indexOfWantedData] > 17) {
return true;
}
return false;
}
I've gotten the conditions to read from the data object correctly using this setup:
instanceProto.onCreate = function() {
//Stuff
socket.onmessage = function(evt) {
instanceProto.updateData(evt.data);
}
//stuff
}
instanceProto.updateData = function(data) {
this.data = data;
}
So this works. Marvelous! My conditions will read this correctly. Then I tried adding in the triggers, changing the last function to this:
instanceProto.updateData = function(data) {
this.data = data;
this.runtime.trigger(cr.plugins_.MyPlugin.prototype.cnds.thisIsATrigger, this);
}
And I get errored out, with a "cannot call trigger of 'undefined'" thing.
(I currently can't get the exact error message since I'm not on the computer I have the work on, but I can post the exact error message later today if necessary)
So. If anyone knows why this.runtime isn't defined inside a function of instanceProto, despite the fact that this looks just like code I see in, for example, the Mouse plugin's onMouseDown trigger, I would love the help.