Skip to content

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

Open
jemmyw wants to merge 1 commit intomainfrom
addDependency
Open

addDependency function on controllers to make it easier to add cleaned up things like events#8
jemmyw wants to merge 1 commit intomainfrom
addDependency

Conversation

@jemmyw
Copy link
Copy Markdown
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