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
Reusable component with private and public handlers #24
Comments
You can use this.run for private handlers in component and can use app.run and the event name with # for public handlers. class CounterComponent extends Component {
state = 0;
view = (state) => <div>
<h1>{state}</h1>
<button onclick={() => this.run("-1")}>-1</button>
<button onclick={() => this.run("+1")}>+1</button>
<button onclick={() => app.run("#-1")}>-1 All</button>
<button onclick={() => app.run("#+1")}>+1 All</button>
</div>;
update = {
'+1': (state) => state + 1,
'-1': (state) => state - 1,
'#+1': (state) => state + 1,
'#-1': (state) => state - 1
};
} |
I am using HTML string. I was not able to invoke a handler via this.run. class Counter extends Component {
constructor() {
super();
this.state = 0;
this.view = state => `<div>
<h1>${state}</h1>
<button onclick="${(e)=>this.run('-1')}">-1</button>
<button onclick='counter.run("+1")'>+1</button>
</div>`;
this.update = {
'+1': state => state + 1,
'-1': state => state - 1
};
}
}
app.webComponent('my-app', Counter); Can I do the same with HTML strings? Thanks! |
Yes, you can. Check out this example: |
Excellent example! |
One more question. |
Component makes the events private. Without using Component, all events are global and public. You will need to define and handle the events carefully. |
Thank you! |
Hello!
First of all thanks for the good framework.
Taking into account the basic example on the first page of Github, could you explain how can I create reusable component that will have private handlers and public, please.
For example I want to create a Counter component. After that I want to create a set of these Counters.
Each of the counters should have internal handlers and several public. If I need increment only one counter I invoke private or personal handler and if I want to increment all counters I invoke public handler that affects all counters via app.run.
Thank you in advance.
The text was updated successfully, but these errors were encountered: