Issue #2007Opened May 10, 2019by kaoz701 reactions

[QUESTION] Adding new traits to component and update current ones with these

Question

I have some custom components created before modifying their traits (for example, I created a new trait for it), these components wont contain the new trait. If I drag a new component into the editor, it will have it.

Steps to reproduce:

  1. Create a custom component and compile the code
  2. Drag this custom component into the editor
  3. Save the changes to DB
  4. Modify the component by adding a new trait and compile again
  5. Load the components from DB
  6. Loaded components wont have the new trait

I load and save all the data into the database: styles, css, components,and html.

load(keys, clb, clbErr) {
                // Load from model
                const content = _model.template[_language.locale];
                delete content.assets; // Delete assets if they exist
                clb(content);
},

Is there a way to make the loaded components aware of the changes in the traits?

Answers (3)

kaoz70November 19, 20191 reactions

@Lakshit-Singhal In that case I think the best option would be to create a custom plugin, and before you initialize GrapesJS, call the API, get the values and pass them in the initialization function via the plugin's options, that way they are present when the trait gets initialized. Check the documentation on how to create a custom plugin, or you can take a look at my plugins: GrapesJS Bootstrap 4 and GrapesJS Swiper Slider to get an idea.

artfMay 12, 20190 reactions

This happens because the editor stores also traits information in components. You can delete this information be extending the component and updating its toJSON method

const typeModel = domc.getType('my-type').model;

domc.addType('my-type', {
	model: {
		...
		toJSON() {
			const result = typeModel.prototype.toJSON.apply(this);
			delete result.traits;

			return result;
		}
	}
})

ps: I think I'll remove traits from JSON in the next release. Perhaps many versions back that would have made sense, but now I'd not even recommend storing stuff in Traits

ahmedeldeeb25-zzMay 12, 20190 reactions

@artf this removed custom traits !! as I mentioned in #2007

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.