Issue #1777πŸ’¬ AnsweredOpened February 6, 2019by bm2ilabs0 reactions

How to dynamically change the DomComponents ?

Quick answerby artf

You don't need to extend the model in this way you can just update the toHTML method if the current instance, eg.

Read full answer below ↓

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)

artfβ€’ February 8, 2019

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;
lock[bot]β€’ February 9, 2020

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.

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...

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins β†’
Premium option

Premium plugins ship with support, regular updates, and production-ready features β€” save days of integration work.

Browse premium plugins β†’

Related tutorials

In-depth guides on the same topic.

All tutorials β†’

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.