When defining callbacks it’s good practice to bind them to the current context by using bindenv(). However, when defining such callback within the constructor of a class (eg assign a handler that does not get injected via the constructor params), which is when being called trying to use instance fields, I don’t seem to get it to work. Is there a trick to this ?
I got it to work via a simple but not so clean code trick: I include a “name” parameter in the class constructor which needs to be made identical to the instance name when declaring it (eg. localstore <- spiFlash(“localstore”, …, … ) .
You can then in the callback reference the instance variables and methods via getroottable()[name].
In general bindenv(this) works in class constructors: here it is in our example Button class https://github.com/electricimp/reference/blob/master/hardware/button/button.nut
Could you maybe post an example of something which you’d like to work, but which doesn’t?
Peter