grapesjs icon indicating copy to clipboard operation
grapesjs copied to clipboard

How to call a function which is outside the component in script function of component.

Open suchithmahadi opened this issue 9 years ago • 2 comments

Please help in solving the below issue. How to call a function which is outside the component in script function of component. ex: demo.myFunction function should be called in component script, but script is not able to recognize demo variable. Where as demo.myFunction can be called in init function.

grapesjs.plugins.add('my-plugin', function(editor, options) {

var demo = {
        this.myFunction(id){
        alert(this.id);
    }
}


var domComponents = editor.BlockManager;
domComponents.add('my-block', {
    id: 'myComp',
    label: 'Custom',
    category: 'plugin',
    attributes: {
        class: 'fa fa-square-o'
    },
    style: {
        padding: '100px'
    },
    content: {
        type: 'myComp'
    }
});


var comps = editor.DomComponents;


var defaultType = comps.getType('default');
var defaultModel = defaultType.model;
var defaultView = defaultType.view;

comps.addType('myComp', {
    model: defaultModel.extend({
            init(){
                // working fine
                demo.myFunction(this.id);
            },

            defaults: Object.assign({}, defaultModel.prototype.defaults, {
                removable: true,
                draggable: true,
                style: {
                    // 'background-color':'yellow',
                },
                editable: true,
                copyable: true,
                badgable: true,
                highlightable: true,
                resizable: true,
                droppable: true,
                content: `<svg style="width:90%;">
                </svg>`,
                script: function() {
                    console.log(this);
                    //not working
                    demo.myFunction(this.id);
                },

            })
        },
    // Define the View
    view: defaultType.view.extend({
        render: function() {
            defaultType.view.prototype.render.apply(this, arguments);
            return this;
        },
    }),
});

})

Thank you.

suchithmahadi avatar Oct 09 '17 10:10 suchithmahadi

You'll find the answer here #358

artf avatar Oct 09 '17 11:10 artf

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.

lock[bot] avatar Sep 18 '19 10:09 lock[bot]