[Question] How to dynamically change the DomComponents ?
Question
Hello , Thank you for this great library , i am working with a newsletter system that get content from the database and generate the latest articles in the site , i created a dynamic block with dynamic type that has source setting , when the user add the source it send a request to the url and it should get the content from and change the block to what the content is from the request .
DomComponents
comps.addType('dynamic', {
model: defaultModel.extend({
defaults: Object.assign({}, defaultModel.prototype.defaults, {
tagName: 'div',
class: 'dynamic-content',
badgable: true,
highlightable: true,
editable: true,
droppable: false,
draggable: true,
removable: true,
traits: [{
type: 'url',
label: 'Source',
name: 'source',
}],
}),
updated(property, value, prevValue) {
if(value.source != null) {
axios.get(value.source).then(function(response) {
var content = response.data;
var originalMap = comps.getType('dynamic');
originalMap.model.extend({
// Override how the component is rendered to HTML
toHTML: function () {
return content;
}
})
console.log(originalMap.view.extend({
}))
}).catch(function(error) {
console.log(error)
})
}
},
toolbar: [
{
attributes: {class: 'fa fa-arrows'},
command: 'import-content',
}
],
},
{
isComponent(el) {
if (el.tagName == 'DIV' && el.dataset.dynamic == "1") {
return {type: 'dynamic'};
}
},
}),
// Define the View
view: defaultView.extend({
events: {
'keydown': function(e) {
if (e.key == 'Backspace') {
e.target.parentNode.removeChild(e.target);
}
}
}
}),
});
Block
blockManager.add('dynmaic', {
id: 'dynmaic',
label: 'Dynmaic Content',
category: 'Extra',
textable: 1,
attributes: {class: 'fab fa-3x fa-react'},
void: true,
highlightable: true,
draggable: true,
droppable: true,
removable: true,
editable: true,
content: {
type: 'dynamic',
attributes: {
'data-dynamic': 1,
value: 'dynamic'
}
},
});
Answers (2)
You don't need to extend the model in this way
var content = response.data;
var originalMap = comps.getType('dynamic');
originalMap.model.extend({
...
toHTML: function () { ... }
})
you can just update the toHTML method if the current instance, eg.
editor.getSelected().toHTML = () => newContent;
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #493
[QUESTION] Can i set content dynamically from block API ?
Hi @artf , very great web builder framework and keep it up :+1: code : As you can see, can i make tag dynamically from my backend? i use EJ...
Issue #1674
Render block after trait action .
I have created new block called sidebar that have dynamic updates by trait component ,trait contain checkboxes to dynamic change content by...
Issue #1499
[Question] Is it possible to perform dom manipulation through the block's contents script?
Hey! First of all thank you so much to build such a great and versatile tool. Is it possible that I can manipulate the dom elements of the...
Issue #1471
[Question] Blockmanager and Image
Hi! I use this code to add a new block: blockManager.add("image", { label: 'Image', category: 'Basic', attributes: { class: "gjs-fonts gjs-...
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.