-
For example, something like the following is acceptable or there is a better (more efficient) way? ...
return html`
<div @mouseenter=${this.hoverEnabled ? this._mouseEnter.bind(this, this.arg) : undefined}
@mouseleave=${this.hoverEnabled ? this._mouseLeave.bind(this, this.arg) : null}
@wheel=${this.mouseWheelEnabled ? this._mouseWheel : null}
... >
...
`
... I have also thought something like the following but it does not work for some reason (why?). ...
return html`
<div ${this.hoverEnabled ? html`@mouseenter=${this._mouseEnter.bind(this, this.arg)}` : ''}
${this.hoverEnabled ? html`@mouseleave=${this._mouseLeave.bind(this, this.arg)}` : ''}
${this.mouseWheelEnabled ? html`@wheel=${this._mouseWheel}` : ''}
... >
...
`
... Tia |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
The first approach works - you are changing the bound event handler based on the Also, you don't need to bind your event handlers to return html`
<div @mouseenter=${this.hoverEnabled ? this._mouseEnter : undefined}
@mouseleave=${this.hoverEnabled ? this._mouseLeave : undefined}
@wheel=${this.mouseWheelEnabled ? this._mouseWheel : undefined}
></div>` |
Beta Was this translation helpful? Give feedback.
-
@justinfagnani Thanks a lot for your response.
Also, after removing ...
return html`
<div ${this.hoverEnabled ? `@mouseenter=${this._mouseEnter.bind(this, this.arg)}` : ''}
${this.hoverEnabled ? `@mouseleave=${this._mouseLeave.bind(this, this.arg)}` : ''}
${this.mouseWheelEnabled ? `@wheel=${this._mouseWheel}` : ''}
... >
...
`
...
I have not managed to use arguments/parameters in event handlers without ...
return html`
<div @mouseenter=${this.hoverEnabled ? this._mouseEnter(this.arg) : undefined} ${''/** It does not work as expected */}
@mouseleave=${this.hoverEnabled ? this._mouseLeave(this.arg) : undefined} ${''/** It does not work as expected */}
@wheel=${this.mouseWheelEnabled ? this._mouseWheel : undefined} ${''/** It works */}
... >
...
`
_mouseEnter(arg) {
arg ? doSomething : doNothing;
...
requestUpadte();
}
_mouseLeave(arg) {
arg ? doSomething : doNothing;
...
requestUpadte();
}
_mouseWheel(event) {
event.preventDefault();
...
requestUpadte();
}
... |
Beta Was this translation helpful? Give feedback.
-
As regards, accessing
render() {
return html`
<button @click=${this._commonMethod.bind(this, 'fwd')}>Fwd</button>
<button @click=${this._commonMethod.bind(this, 'bwd')}>Bwd</button>
<button @click=${this._commonMethod.bind(this, 'up')}>Up</button>
<button @click=${this._commonMethod.bind(this, 'down')}>Down</button>
<button @click=${this._commonMethod.bind(this, 'left')}>Left</button>
<button @click=${this._commonMethod.bind(this, 'right')}>Right</button>
<button @click=${this._commonMethod.bind(this, 'here')}>Here</button>
<button @click=${this._commonMethod.bind(this, 'there')}>There</button>
<button @click=${this._commonMethod.bind(this, 'in')}>In</button>
<button @click=${this._commonMethod.bind(this, 'out')}>Out</button>
`
}
_commonMethod(arg) {
if (arg === 'fwd') doFwd;
//...
if (arg === 'Out') doOut;
// common code
}
} In that case, it is required render() {
return html`
<button id='fwd' @click=${this._commonMethod}>Fwd</button>
<button id='bwd' @click=${this._commonMethod}>Bwd</button>
<button id='up' @click=${this._commonMethod}>Up</button>
<button id='down' @click=${this._commonMethod}>Down</button>
<button id='left' @click=${this._commonMethod}>Left</button>
<button id='right' @click=${this._commonMethod}>Right</button>
<button id='here' @click=${this._commonMethod}>Here</button>
<button id='there' @click=${this._commonMethod}>There</button>
<button id='in' @click=${this._commonMethod}>In</button>
<button id='out' @click=${this._commonMethod}>Out</button>
`
}
_commonMethod(event) {
const arg = event.target.id || event.target.textContent.toLowerCase();
if (arg === 'fwd') doFwd;
//...
if (arg === 'Out') doOut;
// common code
}
} Which one will be used, it is more a matter of taste than efficiency/better practice, isn't it? Are there any chances event handlers/methods with arguments to be supported directly in the Tia |
Beta Was this translation helpful? Give feedback.
As regards, accessing
this.arg
in the event listener methods, IMHO, it might not be very practical when:x
,y
,z
,k
,m
,n
,p
) which they are changed into relatively long property names (egthis._xScale
,this._yScale
,this._zScale
,this._xDeflection
,this._yDeflection
,this._zDeflection
,this._force
) and appear many times with result in making code "ugly'.