Issue #2808💬 AnsweredOpened May 27, 2020by robertraf1 reactions

Custom type component in production environment

快速解答by Ju99ernaut1

onRender simply specifies what should happen when you successfully place a component onto the canvas, whereas script is code that will be generated in your html inside script tags. Something like:

Read full answer below ↓

Question

Hi, I have a question, when I create a custom type component from the editor, what would be the next step to show that component in my application? Currently I am saving the component data in firebase with the firestore plugin, the attributes that are stored in the object are the following: gjs-assets, gjs-components, gjs-css, gjs-html and gjs.styles.

What would be next if I want to use these components in my production environment? Should I use a javascript framework or library? or maybe the same grape editor without the ability to edit, thanks.

Answers (3)

Ju99ernautMay 31, 2020

onRender simply specifies what should happen when you successfully place a component onto the canvas, whereas script is code that will be generated in your html inside script tags.

Something like:

<script>
//...
var items = document.querySelectorAll('#someid');

for (var i = 0; len = items.length; i < len; i++) {
   (function(){
     //minified version of script in component definition 
   })()
}
//...
</script>
Ju99ernautMay 28, 2020

I think in a production environment all you need is the html and css code generated by grapesjs.

robertrafMay 30, 2020
代码片段TEXT
I understand that part, but if I want to render the component I have created it doesn't work. I'll explain a little, I have created a type of component that renders data from an endpoint, if I use the html and css code it doesn't work.

Custom Component Type
`  editor.DomComponents.addType('group', {
        model: {
            defaults: {
                // The tag name that will be used in the final code
                tagName: 'div',
                attributes: "",
                // You would use traits to customize custom properties
                traits: [
                    {
                        type: 'select',
                        changeProp: 1,
                        name: 'customProp',
                        options: []
                    }
                ]
            },
            init() {
                let trait_customProp = this.get('traits').where({ name: 'customProp' })[0];
                trait_customProp.set('options', start_options);
                if (Object.keys(this.getAttributes()).length === 0) {
                    console.log("Objeto init vacío");
                } else {
                    trait_customProp.set('value', this.getAttributes().value);
                }
                this.on('change:customProp', () => this.view.onRender());
            }
        },
        view: {
            // eg. You can customize the tag in the canvas
            // By default, the view will use the same tag of the model
            tagName: 'div',

            async onRender() {

                //Retrieving trait value

                let trait_value = this.model.get('traits').where({ name: 'customProp' })[0].attributes.value;

                if (trait_value === "") {
                    console.log("Attribute's value is empty");
                } else {
                    console.log(`Attribute's value: ${trait_value}`);
                    this.model.addAttributes({ value: trait_value });

                    //Rendering Data
                    try {
                        for (option of start_options) {
                            if (Number(trait_value) === option.value) {
                                this.$el.empty();
                                let response = await axios.get(`http://localhost:8100/dmxConnect/api/documents/retrieveXcollection.php?idcollection=${trait_value}`);
                                this.model.addAttributes({ data: response.data.query1 });
                                if (response.data.query1.length > 0) {
                                    //Muchos data, inentendible!
                                    this.model.getAttributes().data.forEach(res => {
                                        JSON.parse(res.data).forEach(doc_data => {
                                            //console.log(doc_data.userData); 
                                            this.$el.append('<h1>' + doc_data.userData[0] + '</h1>');
                                        });
                                    });
                                } else {
                                    this.$el.append(`<h1>404 ${option.name}</h1>`);
                                }
                            }
                        }
                    } catch (e) {
                        console.log(e);
                    }
                }

            }

        }
    }
    );

    // A block for the group type
    editor.BlockManager.add('test-render', {
        label: 'Test render',
        content: {
            type: 'group'
        },
        category: 'Custom'
    });`

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 →

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.