Issue #2549Opened February 4, 2020by rjrodriguezalvarez977 reactions

[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)

artfFebruary 4, 20204 reactions

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}>`;
	}
}
artfFebruary 4, 20202 reactions

Did you try something like this?

view: {
	init() {
		this.listenTo(this.model, 'change:defaultPrice', this.render);
	},
	onRender() {
		this.el.innerHTML = this.model.get("defaultPrice");
	}
}
rjrodriguezalvarez97February 4, 20201 reactions

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.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.