click binding question
Question
Hello everyone, I'm new to all of this and I've been going through the documentation but I still dont understand how to bind click event. I have 2 files the first file is
var editor = grapesjs.init({
autorender: 0,
container : '#gjs',
components: '<input type="password" class="hello"/> <button handleClick> hello </button>', //first attemp
components: '<input type="password" ondblclick="dbclick"/> <button handleClick> hello </button>', //second attemp
style: '.txt-red{color: red}',
});
and second file is
var comps = editor.DomComponents;
var defaultType = comps.getType('default');
var defaultModel = defaultType.model;
var defaultView = defaultType.view;
var inputTypes = [
{value: 'text', name: 'Text'},
{value: 'email', name: 'Email'},
{value: 'password', name: 'Password'},
{value: 'number', name: 'Number'},
];
comps.addType('input', {
model: defaultModel.extend({
defaults: Object.assign({}, defaultModel.prototype.defaults, {
draggable: 'form, form *',
droppable: false,
traits: ['name', 'placeholder', {
type: 'select',
label: 'Type',
name: 'type',
options: inputTypes,
},{
type: 'checkbox',
label: 'Required',
name: 'required',
}],
}),
},
{
isComponent: function(el) {
if(el.tagName == 'INPUT'){
return {type: 'input'};
}
},
}),
view: defaultType.view.extend({
events: {
click: 'handleClick',
dblclick .hello: function(){ //this doesn't work unexpected token error
dblick function() // this didn't seem to work either
alert('Hi!');
}
},
randomHex: function() {
return '#' + Math.floor(Math.random()*16777216).toString(16);
},
handleClick: function(e) {
this.model.set('style', {color: this.randomHex()}); // <- Affects the final HTML code
this.el.style.backgroundColor = this.randomHex(); // <- Doesn't affect the final HTML code
},
render: function () {
defaultType.view.prototype.render.apply(this, arguments);
this.el.placeholder = 'Text here'; // <- Doesn't affect the final HTML code
return this;
},
}),
});
editor.render();
Does anyone have an example of binding?
Answers (2)
Hi @NHKK if your goal was to extend input component with some binded event you were almost there (you just need to use comps.addType)
//...
comps.addType('input', {
//..
view: defaultType.view.extend({
events: {
click: 'handleClick',
dblclick: 'someOtherFunction',
// or
dblclick: function() {...}
},
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.
Issue #1897
[Question] view vs code
Hello...and sorry for my english! First of all, congratulations for this fantastic framework. I'm trying to use GrapeJS to create my own wo...
Issue #384
Create and Edit Template into Database
This is my code to Create a new template and insert the inline html with style into the database : I am able to insert the template-data in...
Issue #519
No Blocks by default
Using this code I get an editor but there are no blocks. I also do not get the icons for the screen size but other icons are there. As you...
Issue #507
uploadFile being called twice on drop file?
I have an editor initialized as so: If I add an image to the canvas and try to upload a new image by dragging it into the drop area in the...
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.