Issue #1459Opened September 25, 2018by gabrigcl1 reactions

[QUESTION] How to store "data-" attributes into the html tag of the component

Question

Hi, I'm developing a plugin for charts using chart.js. In order to the generated template render the chart from a data model generated by the plugin, I need to store this model into the html tag of the component. The model used by the chart is like this:

{
    "type": "line",
    "data": {
        "labels": ["Rótulo 1", "Rótulo 2", "Rótulo 3", "Rótulo 4"],
        "datasets": [{
            "label": "Dataset 1",
            "data": [3, 9, 8, 2],
            "backgroundColor": "rgba(252,111,116, 0.7)",
            "borderColor": "rgba(35,100,224, 0.7)",
            "borderWidth": 2,
            "showLine": true,
            "pointStyle": "circle"
        }]
    }
}

And have to be stored this way:

<canvas class="gjs-chart" data-gjs-chart-model="
{
    'type': 'line',
    'data': {
        'labels': ['Rótulo 1', 'Rótulo 2', 'Rótulo 3', 'Rótulo 4'],
        'datasets': [{
            'label': 'Dataset 1',
            'data': [3, 9, 8, 2],
            'backgroundColor': 'rgba(252,111,116, 0.7)',
            'borderColor': 'rgba(35,100,224, 0.7)',
            'borderWidth': 2,
            'showLine': true,
            'pointStyle': 'circle'
        }]
    }
}">

However, I don't know how to do this. If I do:

this.model.set('data-gjs-chart-model', JSON.stringify(theChartModel));

the html attribute "data-gjs-chart-model" is not stored in the "html" section of the storage, only in the "components" section. This way, the generated chart only works on a grapesjs environment, but doesn't work if I take just the html and css of the generated template and make a page outside a grapesjs environment.

Answers (2)

gabrigclSeptember 27, 20181 reactions

I have seen there's

component.setAttributes({ id: 'test', 'data-key': 'value' });

method, I'll test this and close this issue.

lock[bot]September 27, 20190 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.