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

addDependency function on controllers to make it easier to add cleaned up things like events #8

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jemmyw
Copy link
Contributor

@jemmyw jemmyw commented Oct 3, 2023

class MyController extends ApplicationController {
  initialize() {
    // Dependency callbacks can be added next to the initialization code
    document.addEventListener("keydown", handleKeyDown);
    this.addDependency(() => document.removeEventListener("keydown", handleKeyDown));

    // observe is automatically cleaned up
    this.observe(() => { console.log(this.state.someValue); });

    // Dependencies can be named so they can be trigged any time
    const connection = new Connection();
    this.addDependency("close_connection", () => connection.close());
  }

  actionClose() {
    this.resolveDependency("close_connection");
  }

  handleKeyDown = (event) => {
  }
}

@percyhanna percyhanna marked this pull request as ready for review November 16, 2023 18:50
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

Successfully merging this pull request may close these issues.

2 participants