Re-render view in editor's canvas whenever Trait value changes
Question
Is there any possible way in grapesjs that whenever a user changes the value of a Trait of a custom component the render function should listening or be re-render?
I'm building a custom component for dynamic contents. The custom component code is:
domComps.addType('dynamicImage', {
model: dModel.extend({
defaults: Object.assign({}, dModel.prototype.defaults, {
traits: [
// strings are automatically converted to text types
'src',
'title',
{
label: 'Dummy Image',
name: 'data-dummyimage',
value: 'http://localhost/templates/assets/logo.png'
}
],
}),
}, {
isComponent: function (el) {
return { type: 'image' }
}
}),
view: dView.extend({
events: { },
render: function () {
dView.prototype.render.apply(this, arguments);
this.el.src = this.el.dataset.dummyimage;
return this;
}
})
});
I want that whenever user change the value of Trait called Dummy Image the render function should run and effect the src value accordingly in the canvas result for img tag.
This is to only effect the view in editor's canvas but not in model.
Can somebody help me to solve this?
Thanks in advance. :)
Answers (3)
Update your view in this way
view: dView.extend({
init() {
this.listenTo(this.model, 'change:attributes:data-dummyimage', this.render)
},
...
})
After 2 days this saved my life. Thanks to both of you!
But properties of html are not editable in canvas.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1472
[QUESTIONS] Custom Code block and JS
Wondering if there is a limitation or is it actually possible to create a block that allows the user of the editor to write custom javascri...
Issue #577
Only allow one component of specific type on editor
is there way to not allow to drop any specific component more than one time on page? I have custom component and user cannot drop it more t...
Issue #1679
How to render component in canvas using trait.
Hi Artf ,Thanks for your great tool,I have created test case for my problem https://js.do/code/271771 I have created custom tool sidebar an...
Issue #1424
Checkbox trait value is rendered differently in view (render()) vs model (getHtml())
I have a custom component with a checkbox trait 'show-name'. When the trait value is 'true', the component renders in the view to the attri...
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.