[QUESTION] Update component view on property change
Question
Hi everyone, I'm trying to create a component that will update it's view based on a property value
editor.DomComponents.addType("price-value-type", {
isComponent: el => {
if (el.tagName == "div" && el.hasAttribute("price-value")) {
return { type: "price-value-type" };
}
},
model: {
defaults: Object.assign({}, defaultProperties, {
defaultPrice: 0,
name: "Price",
classes: ["price"],
components: model => {
return `${model.get("defaultPrice")}`}
})
},
extendView: 'default',
extendFnView: ['render'],
view: {
init() {
console.log(this.render);
this.listenTo(this.model, 'change:defaultPrice', this.render);
},
// render() {
// this.el.innerHTML = this.model.get("defaultPrice");
// }
}
});
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 #3014
BUG: Move component from Child
Hi, i'm trying to make custom components composed with other components. No problem to do that. I'm facing an issue when i want to move the...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.