Update component view on property change
to update the HTML you have to change toHTML in the model
Read full answer below ↓Question
Hi everyone, I'm trying to create a component that will update it's view based on a property value My approach is bassed on #1227 and on the docs In my use case I don't need the trait, I can just update the property programmatically. But this won't work and will not display any component on the canvas even though they are on the layer manager as we can see here(they show properly with the render function commented) maybe i'm missing something?
Answers (3)
to update the HTML you have to change toHTML in the model
model: {
...
toHTML() {
const tagName = this.get('tagName');
const price = this.get('defaultPrice');
return `<${tagName}>${price}</{tagName}>`;
}
}
Did you try something like this?
view: {
init() {
this.listenTo(this.model, 'change:defaultPrice', this.render);
},
onRender() {
this.el.innerHTML = this.model.get("defaultPrice");
}
}
I’ll give it a try the day after tomorrow. Thanks for your quick replies!
Related Questions and Answers
Continue research with similar issue discussions.
Issue #2614
[QUESTION] Recursive view update of custom component
Hi, I have defined a custom type like: I want to keep the customTag in my model and show the content (HTML) in the view. However, in the co...
Issue #2697
[QUESTION] Problem with custom component type and table cell
Hello! I'm trying to add custom component type: And also I'm adding new block: But when I drop this block on the canvas the <td> tag doesn'...
Issue #2660
[QUESTION] Reinit model's components function
Hello guys, I have a custom component with different structure based on prop's value. How can i recall/reinit model's components function o...
Issue #3228
Component Value is not changing
Hi @artf I've added a new component here is the code on selecting the h1 and h2 tag it's changing the value in the HTML section as you can...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.
Browse free plugins →Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.
Browse premium plugins →Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.