Is it possible to pass a function from html into a lit-element?
up vote
2
down vote
favorite
I have seen examples of passing a function from a parent lit-element to a child one like here - https://medium.com/@westbrook/litelement-to-do-app-1e08a31707a4
But I want the users of my element, not to be forced to create a wrapper element to use my element.
For example, my element is a dialog that computes some value.
I was hoping I could do something like this (html using my element):
<script>
function latLongResult(lat,long)
{
console.log("resulting lat long called");
}
</script>
<lat-long-chooser id="latLongDialog" resultingLatLong=${latLongResult(lat,long)}></lat-long-chooser>
And then in my element:
export class LatLongChooser extends LitElement {
static get properties() {
return {
latDecimalDegrees: Number,
longDecimalDegrees: Number,
resultingLatLong: {
type: Function,
}
};
}
saveConvertedValues() {
console.log("save other values called");
this.resultingLatLong(this.latDecimalDegrees,this.longDecimalDegrees)
}
When I try this, I get JavaScript errors.
javascript polymer lit-element
add a comment |
up vote
2
down vote
favorite
I have seen examples of passing a function from a parent lit-element to a child one like here - https://medium.com/@westbrook/litelement-to-do-app-1e08a31707a4
But I want the users of my element, not to be forced to create a wrapper element to use my element.
For example, my element is a dialog that computes some value.
I was hoping I could do something like this (html using my element):
<script>
function latLongResult(lat,long)
{
console.log("resulting lat long called");
}
</script>
<lat-long-chooser id="latLongDialog" resultingLatLong=${latLongResult(lat,long)}></lat-long-chooser>
And then in my element:
export class LatLongChooser extends LitElement {
static get properties() {
return {
latDecimalDegrees: Number,
longDecimalDegrees: Number,
resultingLatLong: {
type: Function,
}
};
}
saveConvertedValues() {
console.log("save other values called");
this.resultingLatLong(this.latDecimalDegrees,this.longDecimalDegrees)
}
When I try this, I get JavaScript errors.
javascript polymer lit-element
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have seen examples of passing a function from a parent lit-element to a child one like here - https://medium.com/@westbrook/litelement-to-do-app-1e08a31707a4
But I want the users of my element, not to be forced to create a wrapper element to use my element.
For example, my element is a dialog that computes some value.
I was hoping I could do something like this (html using my element):
<script>
function latLongResult(lat,long)
{
console.log("resulting lat long called");
}
</script>
<lat-long-chooser id="latLongDialog" resultingLatLong=${latLongResult(lat,long)}></lat-long-chooser>
And then in my element:
export class LatLongChooser extends LitElement {
static get properties() {
return {
latDecimalDegrees: Number,
longDecimalDegrees: Number,
resultingLatLong: {
type: Function,
}
};
}
saveConvertedValues() {
console.log("save other values called");
this.resultingLatLong(this.latDecimalDegrees,this.longDecimalDegrees)
}
When I try this, I get JavaScript errors.
javascript polymer lit-element
I have seen examples of passing a function from a parent lit-element to a child one like here - https://medium.com/@westbrook/litelement-to-do-app-1e08a31707a4
But I want the users of my element, not to be forced to create a wrapper element to use my element.
For example, my element is a dialog that computes some value.
I was hoping I could do something like this (html using my element):
<script>
function latLongResult(lat,long)
{
console.log("resulting lat long called");
}
</script>
<lat-long-chooser id="latLongDialog" resultingLatLong=${latLongResult(lat,long)}></lat-long-chooser>
And then in my element:
export class LatLongChooser extends LitElement {
static get properties() {
return {
latDecimalDegrees: Number,
longDecimalDegrees: Number,
resultingLatLong: {
type: Function,
}
};
}
saveConvertedValues() {
console.log("save other values called");
this.resultingLatLong(this.latDecimalDegrees,this.longDecimalDegrees)
}
When I try this, I get JavaScript errors.
javascript polymer lit-element
javascript polymer lit-element
asked yesterday
Joelio
2,18242760
2,18242760
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Your element's code is fine, the way you're trying to set the function is what's a bit off.
You see, the syntax you're using would work if you were in a lit-html/lit-element render function (just with a few corrections, it would be .resultingLatLong=${latLongResult}
)
However, since you're in a script at the main level you should do something like this:
<script>
function latLongResult(lat,long){
console.log("resulting lat long called");
}
// do it like this so that it's set as a property, setting it as an attribute would require some rather complicated extra code
document.querySelector('#latLongDialog').resultingLatLong = latLongResult;
</script>
<lat-long-chooser id="latLongDialog"></lat-long-chooser>
Here's a glitch with a minimal example of something similar in action
Thanks, I found that I could also bubble an event up and handle it in the parent, is that the preferred way to do it, or is this preferred?
– Joelio
yesterday
Well, this is a rather react-ish way of doing things, the general consensus seems to be that just bubbling an event is better, because for example, if in a new version you change what resultingLatLong receives when it's called it might break down a lot of things, with an event it's a bit easier to avoid those kind of maintenance hell issues and it also helps for reusability between projects
– Alan Dávalos
yesterday
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Your element's code is fine, the way you're trying to set the function is what's a bit off.
You see, the syntax you're using would work if you were in a lit-html/lit-element render function (just with a few corrections, it would be .resultingLatLong=${latLongResult}
)
However, since you're in a script at the main level you should do something like this:
<script>
function latLongResult(lat,long){
console.log("resulting lat long called");
}
// do it like this so that it's set as a property, setting it as an attribute would require some rather complicated extra code
document.querySelector('#latLongDialog').resultingLatLong = latLongResult;
</script>
<lat-long-chooser id="latLongDialog"></lat-long-chooser>
Here's a glitch with a minimal example of something similar in action
Thanks, I found that I could also bubble an event up and handle it in the parent, is that the preferred way to do it, or is this preferred?
– Joelio
yesterday
Well, this is a rather react-ish way of doing things, the general consensus seems to be that just bubbling an event is better, because for example, if in a new version you change what resultingLatLong receives when it's called it might break down a lot of things, with an event it's a bit easier to avoid those kind of maintenance hell issues and it also helps for reusability between projects
– Alan Dávalos
yesterday
add a comment |
up vote
1
down vote
accepted
Your element's code is fine, the way you're trying to set the function is what's a bit off.
You see, the syntax you're using would work if you were in a lit-html/lit-element render function (just with a few corrections, it would be .resultingLatLong=${latLongResult}
)
However, since you're in a script at the main level you should do something like this:
<script>
function latLongResult(lat,long){
console.log("resulting lat long called");
}
// do it like this so that it's set as a property, setting it as an attribute would require some rather complicated extra code
document.querySelector('#latLongDialog').resultingLatLong = latLongResult;
</script>
<lat-long-chooser id="latLongDialog"></lat-long-chooser>
Here's a glitch with a minimal example of something similar in action
Thanks, I found that I could also bubble an event up and handle it in the parent, is that the preferred way to do it, or is this preferred?
– Joelio
yesterday
Well, this is a rather react-ish way of doing things, the general consensus seems to be that just bubbling an event is better, because for example, if in a new version you change what resultingLatLong receives when it's called it might break down a lot of things, with an event it's a bit easier to avoid those kind of maintenance hell issues and it also helps for reusability between projects
– Alan Dávalos
yesterday
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Your element's code is fine, the way you're trying to set the function is what's a bit off.
You see, the syntax you're using would work if you were in a lit-html/lit-element render function (just with a few corrections, it would be .resultingLatLong=${latLongResult}
)
However, since you're in a script at the main level you should do something like this:
<script>
function latLongResult(lat,long){
console.log("resulting lat long called");
}
// do it like this so that it's set as a property, setting it as an attribute would require some rather complicated extra code
document.querySelector('#latLongDialog').resultingLatLong = latLongResult;
</script>
<lat-long-chooser id="latLongDialog"></lat-long-chooser>
Here's a glitch with a minimal example of something similar in action
Your element's code is fine, the way you're trying to set the function is what's a bit off.
You see, the syntax you're using would work if you were in a lit-html/lit-element render function (just with a few corrections, it would be .resultingLatLong=${latLongResult}
)
However, since you're in a script at the main level you should do something like this:
<script>
function latLongResult(lat,long){
console.log("resulting lat long called");
}
// do it like this so that it's set as a property, setting it as an attribute would require some rather complicated extra code
document.querySelector('#latLongDialog').resultingLatLong = latLongResult;
</script>
<lat-long-chooser id="latLongDialog"></lat-long-chooser>
Here's a glitch with a minimal example of something similar in action
answered yesterday
Alan Dávalos
1,257516
1,257516
Thanks, I found that I could also bubble an event up and handle it in the parent, is that the preferred way to do it, or is this preferred?
– Joelio
yesterday
Well, this is a rather react-ish way of doing things, the general consensus seems to be that just bubbling an event is better, because for example, if in a new version you change what resultingLatLong receives when it's called it might break down a lot of things, with an event it's a bit easier to avoid those kind of maintenance hell issues and it also helps for reusability between projects
– Alan Dávalos
yesterday
add a comment |
Thanks, I found that I could also bubble an event up and handle it in the parent, is that the preferred way to do it, or is this preferred?
– Joelio
yesterday
Well, this is a rather react-ish way of doing things, the general consensus seems to be that just bubbling an event is better, because for example, if in a new version you change what resultingLatLong receives when it's called it might break down a lot of things, with an event it's a bit easier to avoid those kind of maintenance hell issues and it also helps for reusability between projects
– Alan Dávalos
yesterday
Thanks, I found that I could also bubble an event up and handle it in the parent, is that the preferred way to do it, or is this preferred?
– Joelio
yesterday
Thanks, I found that I could also bubble an event up and handle it in the parent, is that the preferred way to do it, or is this preferred?
– Joelio
yesterday
Well, this is a rather react-ish way of doing things, the general consensus seems to be that just bubbling an event is better, because for example, if in a new version you change what resultingLatLong receives when it's called it might break down a lot of things, with an event it's a bit easier to avoid those kind of maintenance hell issues and it also helps for reusability between projects
– Alan Dávalos
yesterday
Well, this is a rather react-ish way of doing things, the general consensus seems to be that just bubbling an event is better, because for example, if in a new version you change what resultingLatLong receives when it's called it might break down a lot of things, with an event it's a bit easier to avoid those kind of maintenance hell issues and it also helps for reusability between projects
– Alan Dávalos
yesterday
add a comment |
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
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265266%2fis-it-possible-to-pass-a-function-from-html-into-a-lit-element%23new-answer', 'question_page');
}
);
Post as a guest
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
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
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