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
KVar not binding if Element ID is changed #164
Comments
well, in fact, the src attribute is not changing programmatically even if I use a simple string as a value, it only works if given the value in the constructor |
Hmm, will investigate - appreciate the bug report. |
thanks, and "changing src by javascript works" |
Hmm, I'm having trouble replicating the bug. Here is what I tried: import kweb.state.KVar
fun main() {
Kweb(port = 5415) {
doc.body.new {
val counter = KVar(1)
val imageString = counter.map { "$it.img" }
img().setAttribute("src", imageString)
button().let { button ->
button.text("Increment")
button.on.click {
counter.value++
}
}
}
}
} The image is broken because it's not a valid URL, but looking at the inspector I do see Would it be possible for you to share a complete |
Oh, when you change the value of |
No, I'm using |
I tried your snippet and tested it, and I think I found what caused it to break. - import kweb.*
import kweb.state.KVar
fun main() {
Kweb(port = 5415, debug = true)
{
doc.body.new {
val counter = KVar(1)
val imageString = counter.map { "$it.img" }
img().setAttribute("src", imageString)
// .setAttributeRaw("id","imgFile")
//input(type = InputType.number).setAttributeRaw("id","inp").setAttribute("max", imageString) //any element after this are not rendered.
//input(type = InputType.number).setAttributeRaw("id","inp") //elements will be rendered
input(type = InputType.number).setAttribute("max", imageString).setAttributeRaw("id","inp") //Elements will be rendered but KVar is somehow not binded
button().let { button ->
button.text("Increment")
button.on.click {
counter.value++
}
}
button().text("click me")
}
}
} What I Found -
|
input(type = InputType.number).let {
it.setAttributeRaw("id","inp") // if ID is set first, then elements will not be rendered , however if no ID is set, KVar binds correctly
it.setAttribute("max", imageString) //If Kvar is set first then ID, in this case Elements will be rendered, but still KVal will not be binded
} |
also changing the value using javaScript doesn't update the binded KVar - |
Ah, yes - Kweb relies on the I'll need to give it some thought, it might be necessary to disallow May I ask the reason you need to change an |
Re: This is the relevant code: https://github.com/kwebio/kweb-core/blob/master/src/main/kotlin/kweb/prelude.kt#L214 |
yes, I figured, although when KVar is binded, changing via javascript seems redundant, in my case I need to change it using javascript at least once. |
I got around it using, the id that Kweb assigns, and I think this behavior can be worked around most of the time. and setAtrribute/setAttributeRaw() for id should not be restricted, as not always KVar will be used. although this statement should be made into docs that this doesn't work so don't set id if using KVar on the element. |
and I would like some help, currently, I'm reading a file from a [input type= file], so I'm using FileReader API, which doesn't let me properly return the result data so I have to manipulate the dom in the Reader's Load event which is javascript code. that's why my src unbinds from KVar even if I worked around ID. fun showImage(idOfImg: String): String = """function previewFile(e) {
const preview = document.getElementById("$idOfImg");
const file = e.files[0];
const reader = new FileReader();
reader.addEventListener("load", function () {
preview.src = reader.result;
}, false);
if (file) {
reader.readAsDataURL(file);
}
}""" and here's how I'm using it. - doc.body.new {
input(InputType.file).let {
it.setAttributeRaw("onChange", "previewFile(this);")
}
val imgElement = img()
doc.head.new {
element("script").text(showImage(imgElement.id))
}
} although working, I would want it to do in Kweb/Kotlin style. |
I'm looking to do something Like this - var imageString = KVar("")
doc.body.new {
input(InputType.file).let { fileInput ->
fileInput.on.change {
val file = fileInput.files[0]
val reader = FileReader()
reader.on.load {
imageString = reader.result
}
reader.readAsDataURL(file);
}
}
img().setAttribute("src", imageString)
} |
Sorry for the delayed response. This probably belongs in a separate issue, but Kweb doesn't currently have direct support for You're welcome to create a separate issue for |
thank you for the update, the recent commit fixed the issue of KVar not binding when ID is changed, and the elements are also rendered correctly now, however, changing value by javascript such as my code changing src by javascript still causes KVar to unbind. and as you said before, we should separate these issues, therefor I'm closing this one as resolved. |
I tried to bind variable of type KVar, but it is not binding to the "src" attribute -
var imageString: KVar<String> = KVar("")
img("").setAttribute("src", imageString)
I also tried -
But to no avail. the value of imageString doesn't affect the src attribute. not even one directional
I also read the doc about img, and it said, "Kweb doesn't support internal routing yet, use external source". does that mean what I'm trying to achieve is not possible ?
The text was updated successfully, but these errors were encountered: