`this` is lost whe I use lit-html expressions
up vote
0
down vote
favorite
I'm training with LitElement and li-html. I'm trying to make complex templates with functions and event listener. I have a module for the template module
and another for one component where I use the templates.
I'm having problem with a template for a buttom which I pass a function as parameter and when I clicked on the buttom call the function.
This is works, make the call but the reference for this it's lost. I thought a possible cause maybe arrow function so I rewrite the function like this:
let timerElementOperation = function(operationTimer, operation,that){
    operationTimer.bind(that);
    return html` <button @click=${function(){operationTimer()}}>${operation}</button> `;
}
But the problem carry on. What's happening?
//timer-element.js
class TimerElement extends LitElement{
    ...
    static get properties(){
        return {
            running:  { type: Boolean, reflect: true}
        };
    }
    render(){
        let partialTemplate;
        if( this.isPausable(this.running, this.finished) && this.time > 0 ){
            partialTemplate = Template.timerElementOperation(this.pause, 'pause');
        } else if(!this.running && this.time > 0){
            partialTemplate = Template.timerElementOperation(this.resume,'resume');
        }
    pause(){
        this.running = false; // this is lost.
    }
}
//timer-templates.js
import {html}   from '@polymer/lit-element';
let timerElementOperation = (operationTimer, operation) => {
    return html` <button @click=${()  => operationTimer()}>${operation}</button> `;
}
export timerElementOperation;
javascript polymer web-component es6-class lit-element
add a comment |
up vote
0
down vote
favorite
I'm training with LitElement and li-html. I'm trying to make complex templates with functions and event listener. I have a module for the template module
and another for one component where I use the templates.
I'm having problem with a template for a buttom which I pass a function as parameter and when I clicked on the buttom call the function.
This is works, make the call but the reference for this it's lost. I thought a possible cause maybe arrow function so I rewrite the function like this:
let timerElementOperation = function(operationTimer, operation,that){
    operationTimer.bind(that);
    return html` <button @click=${function(){operationTimer()}}>${operation}</button> `;
}
But the problem carry on. What's happening?
//timer-element.js
class TimerElement extends LitElement{
    ...
    static get properties(){
        return {
            running:  { type: Boolean, reflect: true}
        };
    }
    render(){
        let partialTemplate;
        if( this.isPausable(this.running, this.finished) && this.time > 0 ){
            partialTemplate = Template.timerElementOperation(this.pause, 'pause');
        } else if(!this.running && this.time > 0){
            partialTemplate = Template.timerElementOperation(this.resume,'resume');
        }
    pause(){
        this.running = false; // this is lost.
    }
}
//timer-templates.js
import {html}   from '@polymer/lit-element';
let timerElementOperation = (operationTimer, operation) => {
    return html` <button @click=${()  => operationTimer()}>${operation}</button> `;
}
export timerElementOperation;
javascript polymer web-component es6-class lit-element
 
 
 
 
 
 
 where/how are- this.pauseand- this.resumedefined?
 – Thomas
 Nov 8 at 13:45
 
 
 
 
 
 
 
 
 
 - this.pauseand- this.resumeare defined insid the class TimerElement. You can see the method pause down to render method.
 – Ismael Rodriguez
 Nov 8 at 14:52
 
 
 
 
 
 
 
 
 
 
 
 I guess you should bind- this.pausebefore pass it. Try- Template.timerElementOperation(this.pause.bind(this), 'pause').
 – User 28
 Nov 9 at 6:36
 
 
 
 
 
 
 
 
 
 And what is- Template? How you can call- Template.timerElementOperation? Thanks.
 – User 28
 Nov 9 at 6:37
 
 
 
 
 
 
 
 
 
 Thanks! It's work with- Templale.timerElementOperation(this.pause.bind(this),'pause'). I was setting the bind when the context had already changed. Template is is a import from the file timer-template.js.- import * as Template from '../templates/timer-template.js';
 – Ismael Rodriguez
 Nov 9 at 10:57
 
 
 
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm training with LitElement and li-html. I'm trying to make complex templates with functions and event listener. I have a module for the template module
and another for one component where I use the templates.
I'm having problem with a template for a buttom which I pass a function as parameter and when I clicked on the buttom call the function.
This is works, make the call but the reference for this it's lost. I thought a possible cause maybe arrow function so I rewrite the function like this:
let timerElementOperation = function(operationTimer, operation,that){
    operationTimer.bind(that);
    return html` <button @click=${function(){operationTimer()}}>${operation}</button> `;
}
But the problem carry on. What's happening?
//timer-element.js
class TimerElement extends LitElement{
    ...
    static get properties(){
        return {
            running:  { type: Boolean, reflect: true}
        };
    }
    render(){
        let partialTemplate;
        if( this.isPausable(this.running, this.finished) && this.time > 0 ){
            partialTemplate = Template.timerElementOperation(this.pause, 'pause');
        } else if(!this.running && this.time > 0){
            partialTemplate = Template.timerElementOperation(this.resume,'resume');
        }
    pause(){
        this.running = false; // this is lost.
    }
}
//timer-templates.js
import {html}   from '@polymer/lit-element';
let timerElementOperation = (operationTimer, operation) => {
    return html` <button @click=${()  => operationTimer()}>${operation}</button> `;
}
export timerElementOperation;
javascript polymer web-component es6-class lit-element
I'm training with LitElement and li-html. I'm trying to make complex templates with functions and event listener. I have a module for the template module
and another for one component where I use the templates.
I'm having problem with a template for a buttom which I pass a function as parameter and when I clicked on the buttom call the function.
This is works, make the call but the reference for this it's lost. I thought a possible cause maybe arrow function so I rewrite the function like this:
let timerElementOperation = function(operationTimer, operation,that){
    operationTimer.bind(that);
    return html` <button @click=${function(){operationTimer()}}>${operation}</button> `;
}
But the problem carry on. What's happening?
//timer-element.js
class TimerElement extends LitElement{
    ...
    static get properties(){
        return {
            running:  { type: Boolean, reflect: true}
        };
    }
    render(){
        let partialTemplate;
        if( this.isPausable(this.running, this.finished) && this.time > 0 ){
            partialTemplate = Template.timerElementOperation(this.pause, 'pause');
        } else if(!this.running && this.time > 0){
            partialTemplate = Template.timerElementOperation(this.resume,'resume');
        }
    pause(){
        this.running = false; // this is lost.
    }
}
//timer-templates.js
import {html}   from '@polymer/lit-element';
let timerElementOperation = (operationTimer, operation) => {
    return html` <button @click=${()  => operationTimer()}>${operation}</button> `;
}
export timerElementOperation;
javascript polymer web-component es6-class lit-element
javascript polymer web-component es6-class lit-element
asked Nov 8 at 13:21


Ismael Rodriguez
112
112
 
 
 
 
 
 
 where/how are- this.pauseand- this.resumedefined?
 – Thomas
 Nov 8 at 13:45
 
 
 
 
 
 
 
 
 
 - this.pauseand- this.resumeare defined insid the class TimerElement. You can see the method pause down to render method.
 – Ismael Rodriguez
 Nov 8 at 14:52
 
 
 
 
 
 
 
 
 
 
 
 I guess you should bind- this.pausebefore pass it. Try- Template.timerElementOperation(this.pause.bind(this), 'pause').
 – User 28
 Nov 9 at 6:36
 
 
 
 
 
 
 
 
 
 And what is- Template? How you can call- Template.timerElementOperation? Thanks.
 – User 28
 Nov 9 at 6:37
 
 
 
 
 
 
 
 
 
 Thanks! It's work with- Templale.timerElementOperation(this.pause.bind(this),'pause'). I was setting the bind when the context had already changed. Template is is a import from the file timer-template.js.- import * as Template from '../templates/timer-template.js';
 – Ismael Rodriguez
 Nov 9 at 10:57
 
 
 
add a comment |
 
 
 
 
 
 
 where/how are- this.pauseand- this.resumedefined?
 – Thomas
 Nov 8 at 13:45
 
 
 
 
 
 
 
 
 
 - this.pauseand- this.resumeare defined insid the class TimerElement. You can see the method pause down to render method.
 – Ismael Rodriguez
 Nov 8 at 14:52
 
 
 
 
 
 
 
 
 
 
 
 I guess you should bind- this.pausebefore pass it. Try- Template.timerElementOperation(this.pause.bind(this), 'pause').
 – User 28
 Nov 9 at 6:36
 
 
 
 
 
 
 
 
 
 And what is- Template? How you can call- Template.timerElementOperation? Thanks.
 – User 28
 Nov 9 at 6:37
 
 
 
 
 
 
 
 
 
 Thanks! It's work with- Templale.timerElementOperation(this.pause.bind(this),'pause'). I was setting the bind when the context had already changed. Template is is a import from the file timer-template.js.- import * as Template from '../templates/timer-template.js';
 – Ismael Rodriguez
 Nov 9 at 10:57
 
 
 
where/how are
this.pause and this.resume defined?– Thomas
Nov 8 at 13:45
where/how are
this.pause and this.resume defined?– Thomas
Nov 8 at 13:45
this.pause and this.resume  are defined insid the class TimerElement. You can see the method pause down to render method.– Ismael Rodriguez
Nov 8 at 14:52
this.pause and this.resume  are defined insid the class TimerElement. You can see the method pause down to render method.– Ismael Rodriguez
Nov 8 at 14:52
I guess you should bind
this.pause before pass it. Try Template.timerElementOperation(this.pause.bind(this), 'pause').– User 28
Nov 9 at 6:36
I guess you should bind
this.pause before pass it. Try Template.timerElementOperation(this.pause.bind(this), 'pause').– User 28
Nov 9 at 6:36
And what is
Template? How you can call Template.timerElementOperation? Thanks.– User 28
Nov 9 at 6:37
And what is
Template? How you can call Template.timerElementOperation? Thanks.– User 28
Nov 9 at 6:37
Thanks! It's work with
Templale.timerElementOperation(this.pause.bind(this),'pause').   I was setting the bind when  the context had already changed.  Template is is a import from the file timer-template.js.  import * as Template from '../templates/timer-template.js';– Ismael Rodriguez
Nov 9 at 10:57
Thanks! It's work with
Templale.timerElementOperation(this.pause.bind(this),'pause').   I was setting the bind when  the context had already changed.  Template is is a import from the file timer-template.js.  import * as Template from '../templates/timer-template.js';– Ismael Rodriguez
Nov 9 at 10:57
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53208622%2fthis-is-lost-whe-i-use-lit-html-expressions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
 
where/how are
this.pauseandthis.resumedefined?– Thomas
Nov 8 at 13:45
this.pauseandthis.resumeare defined insid the class TimerElement. You can see the method pause down to render method.– Ismael Rodriguez
Nov 8 at 14:52
I guess you should bind
this.pausebefore pass it. TryTemplate.timerElementOperation(this.pause.bind(this), 'pause').– User 28
Nov 9 at 6:36
And what is
Template? How you can callTemplate.timerElementOperation? Thanks.– User 28
Nov 9 at 6:37
Thanks! It's work with
Templale.timerElementOperation(this.pause.bind(this),'pause'). I was setting the bind when the context had already changed. Template is is a import from the file timer-template.js.import * as Template from '../templates/timer-template.js';– Ismael Rodriguez
Nov 9 at 10:57