`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;









share|improve this question






















  • 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












  • 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










  • 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















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;









share|improve this question






















  • 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












  • 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










  • 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













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;









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 8 at 13:21









Ismael Rodriguez

112




112












  • 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












  • 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










  • 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










  • 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










  • 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

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)