Issue #1777Opened February 6, 2019by bm2ilabs0 reactions

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

artfFebruary 8, 20190 reactions

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, 20200 reactions

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

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.