Issue #2193Opened August 14, 2019by inaLar2 reactions

[QUESTION] Reset trait on edit the content of a custom component

Question

Hi,

I have a custom component which content can be changed by switching trait select options, but also can be edited similar to the text component type when it is already rendered inside the canvas. Is there an easy way to reset the traits related attribute on edit of a component?

model: {
        defaults:  {
         type: 'tag',
          tagName: 'span',
          droppable: 0,
          editable: 1,
          textable: 1,
          traits: [{
            type: 'select',
            label: 'Custom type',
            name: 'customType',
            options: customTypeOptions,
          }],
        },
		toHTML() {
          return this.get('content');
        },

view: {
	events: {
            focus: 'resetTrait',
	},
	reseTrait() {
	}
}

Answers (3)

inaLarAugust 24, 20192 reactions

@artf, regarding the "Select" I couldn't manage to connect the change in the content and make it work on change of the trait in one flow and also to change it manually by editing it's text/content. The manual change wasn't syncing the trait, but I managed to make it work. A custom text component worked just fine.

As for the documentation from the last release, I am reading it right now and it looks very good, but I haven't seen it when I posted the question.

It may be only me, but I learn by example, so more examples and use cases will be always a plus.

I understand that the product is open and I appreciate your effort. I hope that I will gain more knowledge and will be able to contribute with something in near future.

Cheers.

zgoverAugust 14, 20190 reactions

You could attach an event listener to the content prop of the component, then in the callback fetch the trait or change the value prop of the trait, see example below taken from docs

// Let's update `options` of our `type` trait, defined in Input component
const component = editor.getSelected();
component.getTrait('type').set('options', [
  { id: 'opt1', name: 'New option 1'},
  { id: 'opt2', name: 'New option 2'},
]);
// or with multiple values
component.getTrait('type').set({
  label: 'My type',
  options: [...],
});
inaLarAugust 15, 20190 reactions

Thank you Zachary1748, but I can't use that in my case. Maybe I didn't explained it right. I have a custom made component. The components content can be changed in 3 ways:

  1. When dragged and droped into the canvas (you have a popup with options)
  2. From Settings (also via choosing between options)
  3. By editing its text inside the canvas

Anyway I think I fount a solution - I created a custom data type and binded the events to its change also, not only to the content changes. But I am still testing it.

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

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.