Issue #3329πŸ’¬ AnsweredOpened March 10, 2021by imouou0 reactions

How should I receive custom trait modifications in the script?

Quick answerby Ju99ernaut

You'll need to add the trait to script-props, you can read more here https://grapesjs.com/docs/modules/Components-js.html#passing-properties-to-scripts

Read full answer below ↓

Question

Hi, @artf, this is a great framework, thank you very much.

I am now using it to create components of the mobile BUI framework, and I ran into a problem, I added a custom trait textarea, how can I make this When the data changes, it is triggered in the output script.

editor.TraitManager.addType('textarea', {
    createInput({
      trait,
      component
    }) {
      // Here we can decide to use properties from the trait
      const el=document.createElement('div');
      el.innerHTML = `
      <textarea class="buiv-textarea" name="${options[0].name}" rows="${options[0].rows}">${options[0].value}</textarea>
    `;
      return el;
    },
    onEvent({
      elInput,
      component,
      event
    }) {
// The data change will reach this callback, you can operate the component, but how can we trigger the parameters received in the script? 
    },
    onUpdate({
      elInput,
      component,
      trait
    }) {
    },
  });

// use trait textarea 
editor.DomComponents.addType("slide", {
    model: defaultModel.extend({
      defaults: {
        ...defaultModel.prototype.defaults,
        name: "slide",
        content: `<div class="bui-slide bui-slide-skin01"></div>`,
        datas: '[{image:""}]',
        traits: [{
          type: 'textarea',
          name: 'datas',
          label: "data",
          changeProp: 1,
        }],
        script() {
        
          let parentId=this.id;
          // first time it is [{image:""}],  I want to listen 'datas' change trigger here.
          console.log('{[ datas ]}')

        },
      },
    }
    }),
    view: defaultView.extend({
      init() {
        const props=['loop', 'fullscreen', 'height', 'direction', 'visibleNum', 'scrollNum', 'datas', 'autoplay'];
        
        const reactTo = props.map(prop => `change:${prop}`).join(' ');

        this.listenTo(this.model, reactTo, this.render);

      }
    }),
  });

Answers (4)

imououβ€’ March 13, 2021

Thanks @Ju99ernaut, If it is some built-in features, when modifying the script can receive the value of the parameter, I am currently experiencing problems: custom feature textarea, can trigger changes such as dom, but the exported script can not receive the value of this parameter, even if the script-props are set: "datas".

Ju99ernautβ€’ March 14, 2021

You can try forcing the script to update:

editor.DomComponents.addType('slide', {
   model: {
      // ...
      init() {
        this.on('change:datas', () => this.trigger('change:script'));
      },
   },
   // ...
});
ClaudeCodeβ€’ May 17, 2026

Thanks for reporting this, @imouou.

Great question about How should I receive custom trait modifications in the script?. The recommended approach with Canvas is to use the event-driven API.

Start here:

  1. Check the GrapesJS documentation for your specific module
  2. Look for the on() event listener method
  3. Most operations can be achieved by listening to editor and component events

Common patterns:

// Listen for changes
editor.on('change', () => console.log('something changed'));

// Component lifecycle
editor.on('component:mount', (c) => console.log('component ready', c));
editor.on('component:update', (c) => console.log('component updated', c));

If you're still stuck:

  • Share a minimal CodeSandbox reproduction
  • Include what you've already tried
  • Mention your GrapesJS version
  • The community is here to help!

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.