[HELP] Canvas clears component rendering after Traits value change event
Question
I have created custom components and blocks, but when value of traits change, the rendered element gets invisible from the canvas until it is reloaded.
const DynamicBodyComponent = editor => {
editor.DomComponents.addType('app-body', {
isComponent: el => el.tagName === 'app-body',
view: {
onRender() { // use onRender hook instead of render
const el = document.createElement("div");
el.innerHTML = `
<div>Dynamic Page Content Placeholder (Page Specific Content Will be displayed here..)</div>
<style>
</style>`;
this.el.appendChild(el);
}
},
model: {
defaults: {
editable: true,
droppable: true,
traits: [
// Strings are automatically converted to text types
{type: 'text', name: 'searchPlaceHolder', label: 'Search Text'}
],
attributes: {
'searchPlaceHolder': 'Search Over 1000 Scrimming Good Deals'
}
// As by default, traits are binded to attributes, so to define
// their initial value we can use attributes
},
init() {
this.listenTo(this, 'change:attributes', this.handleChange);
this.handleChange();
},
handleChange() {
const value = JSON.stringify(this.getAttributes());
this.components(`<app-body props='${value}'></app-body>`); //this line is causing issue
},
}
});
editor.BlockManager.add('app-body', {
label: 'Dynamic Page Body',
attributes: {
class: 'fa fa-file-code-o'
},
category:{
label: "Theme",
order: 1
},
content: {
type: 'app-body'
}
});
}
So whenever this.components(`<app-body props='${value}'></app-body>`); this line is executed, the loaded block gets invisible or unrendered. A workaround that i got was to re-render DomComponents after this call:
editor.DomComponents.render()
but that does not seems a good approach, So is there anyother workaround for this problem?
Answers (2)
@mustahsanmustafa I think there is something wring here...
init() {
this.listenTo(this, 'change:attributes', this.handleChange);
this.handleChange();
},
handleChange() {
const value = JSON.stringify(this.getAttributes());
this.components(`<app-body props='${value}'></app-body>`); //this line is causing issue
},
For any new app-body component you're creating a new inside with this this.components('<app-body props='${value}'></app-body>'). The method this.components updates inner content not the component itself... If you need to update a property you should do just this this.set('propName', 'propValue').
You can also listen a single attribute change if you want:
this.listenTo(this, 'change:attributes:search-placeholder', this.handleChange);
ps. By HTML5 specs attribute names SHOULD ONLY be in lower case, so change searchPlaceHolder in something like search-placeholder
Hi there! being more specific in reaction to what attribute is actually changing would help you to reduce the performance issue in your case. cheers!
Related Questions and Answers
Continue research with similar issue discussions.
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 #2549
[QUESTION] Update component view on property change
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 th...
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...
Issue #2839
[Question] I added one component named template in the blocks but when i try to drag another components inside this template component ,it will be dragged but after saving it removes all the components inside it. How to avoid this?
This is code of template component: I debugged it, so i got that : setComponents method in the dom_components clearing it ...but not gettin...
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.