Issue #696💬 AnsweredOpened December 31, 2017by NHKK0 reactions

Click binding question

快速解答by artf

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)

Read full answer below ↓

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)

artfJanuary 4, 2018

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() {...}
	    },
lock[bot]September 18, 2019

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...

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 →

Related tutorials

In-depth guides on the same topic.

All tutorials →

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.