[SOLVED] How do you use 'this.app' inside a 'this.app' function?

how do you use ‘this.app’ inside a ‘this.app’ function?

Example:

this.app.on('select', function(x) {
    this.app.root.find(function(node) {
        return node.name;
    });
});

the nested this.app won’t work, what can I do to make it work? I’m guessing the ‘this’ gets screwed up during nesting, but I don’t know how to fix it. I’ve tried my best to avoid this problem by using this.app without nesting, but now I really need to use it inside an event handler.

nevermind it’s just this.root.find in this case, but I wish there was a less confusing way to handle this, if anyone knows please reply here

The this inside the anonymous function is not the same this outside it as they are in different scopes. The typical way of doing the above is to create a new variable within a scope that can be captured by the anonymous and use that instead:

var app = this.app;
this.app.on('select', function(x) {
   app.root.find(function(node) {
        return node.name;
    });
});

ended up doing this

this.app.on('EVENT_SOMETHING', this.onSomething, this);

then I can use this.app inside the onSomething handler like normal

not sure how it works, but it works

The last parameter on that call is the scope for the callback. As you are passing the current this context, the callback would be called with that context.

The following would probably work too:

this.app.on('select', function(x) {
   this.app.root.find(function(node) {
        return node.name;
    }, this);
});