Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'this' context in event handlers should be a reference to the class where the handler lives #7

Open
timkindberg opened this issue Jun 4, 2015 · 3 comments

Comments

@timkindberg
Copy link
Contributor

When I'm using the Component events config it hooks up the handler with an incorrect this context. I would expect this to reference the App class instance, but it actually references the Home class instance.

@Component({
  selector: 'home'
})
@View({
  template: "<p>Home</p>",
  // Here I'm setting up a 'foo' event
  events: {
    foo: "foo"
  }
})
class HomeComponent {
  constructor() {
    // See here that I'm adding a property to Home
    this.wtf = "wat?";
  }
}

@AsModule('AppModule', [HomeComponent])
@Component({
  selector: "app"
})
@View({
  // Here I'm using the on-foo event and passing a reference to the App's doFoo method
  inline: `<home on-foo="doFoo"></home>`
})
class AppComponent {
  doFoo() {
    // Here is where things fell apart for me and 'this' was not what I expect
    console.log(this) // HomeComponent {wtf: "wat?"}, expected an instance of AppComponent
  }
}

bootstrap(AppComponent)
@hannahhoward
Copy link
Owner

Yes. this is a somewhat known bug (I did a quick and dirty implementation of the events -- should have noted this problem since I knew about it when it was implemented). Unfortunately it's not exactly easy to fix given Angular 1.x's way of working. I intend to give it a fix, but it may take a bit.

@hannahhoward
Copy link
Owner

The work around for now is to bind the function before you pass it. i.e. for the class you listed:

class AppComponent {
  constructor() {
     this.doFoo.bind(this);
  }

  doFoo() {
    console.log(this) // should still be AppComponent
  }
}

@timkindberg
Copy link
Contributor Author

Thanks for responding! For now we are ditching the formal usage of classes and just writing everything in the class constructor. That allows me to use the let self = this; reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants