BUG: Event handler on view not bound to the component
Question
GrapesJS version
- I confirm to use the latest version of GrapesJS
What browser are you using?
Chrome Version 115.0.5790.114
Reproducible demo link
https://jsfiddle.net/mcraf3se/5/
Describe the bug
Hello, just starting with GrapesJS so it is quite likely I am misunderstanding something fundamental. I am struggling with setting the event handlers on the view of a custom component.
How to reproduce the bug? In the linked JS fiddle.
- Drag block My Div onto canvas
- Click inside it (there should be a div with 'stuff').
What is the expected behavior?
Per documentation here the event handlers should be bound to the component and I should be able to use something like this.model. ....
What is the current behavior? When running locally the handler is bound to window. In the JSFilddle this seems to be undefined. In both cases call to this.model.components fails
If is necessary to execute some code in order to reproduce the bug, paste it here below:
# the fiddle should have the code in it but if someone wants to run it locally. Here is the code.
const editor = grapesjs.init({
// Indicate where to init the editor. You can also pass an HTMLElement
container: '#gjs',
// Get the content for the canvas directly from the element
// As an alternative we could use: `components: '<h1>Hello World Component!</h1>'`,
fromElement: true,
// Size of the editor
height: '300px',
width: '100%',
// Disable the storage manager for the moment
storageManager: false,
// Avoid any default panel
panels: { defaults: [] },
blockManager: {
appendTo: '#blocks',
blocks: [
{
id: 'my-div',
label: 'My Div',
content: {
type: 'my-input-type'
}
},
]
},
});
editor.DomComponents.addType('my-input-type', {
// Make the editor understand when to bind `my-input-type`
isComponent: el => el.tagName === 'div',
// Model definition
model: {
defaults: {
tagName: 'div',
// draggable: 'form, form *', // Can be dropped only inside `form` elements
content: 'Stuff',
droppable: false, // Can't drop other elements inside
}
},
view: {
tagName: 'div',
events: {
click: 'clickOnElement'
},
clickOnElement: () => {
console.log(this)
this.model.components('hello')
}
}
});
Code of Conduct
- I agree to follow this project's Code of Conduct
Answers (1)
The error here is using the arrow function, just replace it with clickOnElement() {
Related Questions and Answers
Continue research with similar issue discussions.
Issue #4572
BUG: Page update event doesn't fire
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? chrome Reproducible demo link https://jsfi...
Issue #5245
BUG: Contents of component with editable:false are still editable
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome 114.0.5735.198 Reproducible demo li...
Issue #5633
BUG: Cannot select component in ``component:remove`` handler
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome Version 120.0.6099.225 Reproducible...
Issue #5199
BUG: Changes in component's attributes being reproduced in all the instances instead of just one
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? Chrome v114 Reproducible demo link https:/...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.