Issue #1080💬 AnsweredOpened May 2, 2018by francesca-cicileo0 reactions

Changing component attribute from script

快速解答by artf

You can access nested properties like this var customAttribute = '{[ attributes.customAttribute ]}'; inside the script. You should use model's properties: and then in your script var customAttribute = '{[ customProp ]}'; One more thing, you can't update the variable inside the script and expect it to propagate that ch...

Read full answer below ↓

Question

I am extending the 'text' component by adding a new attribute and a script. When a user types a certain character in the text component, I want the script to change the attribute in the component's model, but I'm not sure how to do that. This is my setup for modifying the text component:

var originalTextComp = domc.getType('text');
domc.addType('text', {
    model: originalTextComp.model.extend({
        defaults: Object.assign({}, originalTextComp.model.prototype.defaults, {
          attributes: {
            customAttribute: false,
          },
          script: function() {
            var customAttribute = '{[ attributes.customAttribute ]}';
            var show = function() {
              if (customAttribute) {
                console.log('show');
              }
            };
            var hide = function() {
              if (customAttribute) {
                console.log('hide');
              }
            };

            var checkEventAndRun = function(e) {
              if (e.which == 219 && !customAttribute) {
                customAttribute = true;
                show();
              } else if (e.which == 221 && customAttribute) {
                hide();
                customAttribute = false;
              }
            };

            this.addEventListener('keyup', checkEventAndRun);
            this.addEventListener('blur', hide);
            this.addEventListener('focus', show);
          },
        }),
    }),
    view: originalTextComp.view
  });

I have two problems: the customAttribute is not being loaded into the script properly, and I don't know how to get the value of the customAttribute in the script to change the value of the custom attribute in the model. I have a listener editor.on('component:update:attributes', model => {}); that does not get called, even when I see 'hide' and 'show' in the console (showing that the customAttribute did get changed in the script). The listener is only called when I first place the component on the canvas (so it is working). Any ideas?

Answers (2)

artfMay 4, 2018

You can access nested properties like this var customAttribute = '{[ attributes.customAttribute ]}'; inside the script. You should use model's properties:

...
customAttribute: '',
script: function() {
...

and then in your script var customAttribute = '{[ customProp ]}'; One more thing, you can't update the variable inside the script and expect it to propagate that change, the script works in a totally separate context #358

lock[bot]September 17, 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.