Grapesjs: New Trait creation. The value is not set?

Created on 25 Jul 2017  路  5Comments  路  Source: artf/grapesjs

I am trying to create a new Trait for a component. The textarea appears but it is not saving when trying to edit back, and the object has no "content" attribute. Why? I am basically using the code from here: https://github.com/artf/grapesjs/wiki/Traits#define-new-trait-type

// Each new type extends the default Trait
editor.TraitManager.addType('content', {
  events:{
    'keyup': 'onChange',  // trigger parent onChange method on keyup
  },

  /**
  * Returns the input element
  * @return {HTMLElement}
  */
  getInputEl: function() {
    var selectedModel = this.target;
    var input = document.createElement('textarea');
    input.value = selectedModel.get('content');
    return input;
  },

  /**
   * Triggered when the value of the model is changed
   */
  onValueChange: function () {
    this.target.set('content', this.model.get('value'));
  }
});

Thank you!

outdated

All 5 comments

Hi @NETCreator-US can you try with this and let me know if it works:

/**
  * Returns the input element
  * @return {HTMLElement}
  */
  getInputEl: function() {
     if(!this.inputEl) {
        var input = document.createElement('textarea');
        input.value = this.target.get('content');
        this.inputEl = input;
      }
      return this.inputEl;
  },

Thank you, @artf ! It works now.

Cool, this worked for me.

Could you please update the example on https://github.com/artf/grapesjs/wiki/Traits#define-new-trait-type with this code. I took some serieus time before i found this bug report.

done

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FlashPapa picture FlashPapa  路  3Comments

YashPrince picture YashPrince  路  3Comments

kosirm picture kosirm  路  3Comments

mathiasbc picture mathiasbc  路  3Comments

Geczy picture Geczy  路  3Comments