How to update the trait value
@abzal0 alright! although the prop approach must work fine, i am not sure why it is not, or i am missing something here! if we want to go with attributes, you must use `addAttributes` for changing the attribute, here is a working example of the procedure: Cheers!
Read full answer below ↓Question
Hello @artf thanks a lot for a great project,
I have created a new type, it is similar to regular link, but the link title updates with ajax. Then updates the trait 'linktitle'. The value of it and the canvas do update, but the trait inside "Settings" still shows the initial value and does not changes.
Part of the code that must update the value:
editor.DomComponents.addType('newLink', {
model: {
defaults: {
components: [
{
highlightable: true,
editable: true,
selectable: true
}],
traits: [
{
type: 'text',
label: 'Url',
name: 'url',
value: 'https://',
changeProp: 0
},
{
type: 'text',
label: 'Title',
name: 'linktitle',
value: 'Link title', //this must be updated
changeProp: 0
}
]}},
view:{
tagName: 'div',
init({ model }) {
this.listenTo(model, 'change:attributes', model => {
//First attempt
editor.getSelected().updateTrait('linktitle', {
type: 'text',
label: 'Title 2',
name: 'linktitle',
value: 'AA', //updating the value to AA
changeProp: 0
});
//shows undefined on canvas but only updates the label, need to update the value
//second attempt
this.model.attributes.attributes.title=' BB ';
//updates the canvas link to BB, but does not update the trait
editor.trigger('component:toggled');
editor.trigger('component:updated');
editor.trigger('component:update:linktitle');
editor.trigger();
});
},
}
}
)
Any suggestion how to update the trait value would be helpful.Answers (3)
@abzal0 alright! although the prop approach must work fine, i am not sure why it is not, or i am missing something here! if we want to go with attributes, you must use addAttributes for changing the attribute, here is a working example of the procedure:
domc.addType('test', {
model: {
init() {
this.listenTo(this, 'change:attributes:linkTitle', () => console.log('linkTitle', this.getAttributes()['linkTitle']))
},
defaults: {
attributes: {
linkTitle: '',
},
style: {
padding: '25px',
margin: '25px',
},
traits: [
{type: 'text', name: 'linkTitle', changeProp: false, label: 'Link Title'}
]
}
},
view: {
init() {
const oldValue = this.model.getAttributes()['linkTitle']
console.log('linkTitle: ', oldValue)
this.model.addAttributes({'linkTitle':oldValue + ' Changed!'})
}
}
})
Cheers!
@pouyamiralayi thanks a lot for your time, this solved the issue:
this.model.addAttributes({'linkTitle':oldValue + ' Changed!'})
you saved so much time, thanks again :)
editor.getSelected().getTrait('linktitle').set('value','CCC');
does not works too
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1113
[QUESTIONS] New trait type is not working with rendered components?
Hello, thanks for the great framework. I'm trying to apply this to my project. I want to add a new trait for existing LINK '<a>' component....
Issue #1679
How to render component in canvas using trait.
Hi Artf ,Thanks for your great tool,I have created test case for my problem https://js.do/code/271771 I have created custom tool sidebar an...
Issue #3328
How can I update the nodeValue (text value) of components by inputs forms
Hello. Grapesjs is a great tool. Thanks for it. I want to update the text value of a component by inputs, how can I do that?? if a user fil...
Issue #2972
FEAT: Disable scripts on canvas
Hello, first of all thanks for a great tool. On my project i hit the problem where I need to disable scripts inside the editor. I found 2 s...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.
Browse free plugins →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.
Tutorial
How to Build a Production GrapesJS Editor: The Complete Walkthrough of Brief, Preset, Plugins, and Services
A complete walkthrough of building a production GrapesJS editor: how to choose a preset, pick plugins, and scope setup services without burning a sprint.
Tutorial
Embed GrapesJS in Your SaaS: A Weekend Guide
Embed GrapesJS in your SaaS and ship a white-label page builder over a weekend. Honest tradeoffs, real code, and the plugins that close the UX gap.
Tutorial
Big Updates: TinyMCE 8 and Placeholder 2.0 for GrapesJS
In May we shipped major updates to two of our most popular GrapesJS plugins — TinyMCE Inline Text Editor and Placeholder.
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.