Model and returned html issue
Question
I have below code to create custom component and wants to retrieve updated html via editor.html() but return wrong html, I am sure I am missing something again.
we need below template returned [482](https://github.com/artf/grapesjs/issues/482)
Codeplex: [Template](http://codepen.io/kmpdev/full/LORdOM/)
`function createPCBoxComp(comps, defaultModel, defaultView) {
comps.addType('pcBox', {
// Define the Model
model: defaultModel.extend({
// Extend default properties
defaults: Object.assign({}, defaultModel.prototype.defaults, {
droppable: false,
void: 0,
src: '',
hoverText: 'Link to curriculum page',
title: ' Curriculum Link',
a_href: 'https://team.global.test/sites/testUni',
target: '_blank',
traits: [{
type: 'text',
label: 'Hover Text',
name: 'hoverText',
placeholder: 'eg. Link to curriculum page',
changeProp: 1,
},
{
type: 'text',
label: 'Title(at bottom of image)',
name: 'title',
placeholder: 'eg. Curriculum Link',
changeProp: 1,
}, {
type: 'text',
label: 'Link to open',
name: 'a_href',
placeholder: 'eg. https://team.global.test/sites/testUni',
changeProp: 1,
}, {
type: 'select',
label: 'Target',
name: 'target',
options: [{
value: '_parent',
name: 'This Window'
},
{
value: '_blank',
name: 'New Window'
},
],
changeProp: 1,
},
{
type: 'text',
label: 'Image Source',
name: 'src',
placeholder: 'eg. https://team.global.test/sites/LearningDevLib/test%20portal%20deliverables%20%20approved/2.%20Business%20and%20Role%20Specific/7.test%20technology/Assets_pics_graphics/t3.1_test_engineering/test_tools_training.jpg',
changeProp: 1,
},
],
}),
initialize(o, opt) {
defaultModel.prototype.initialize.apply(this, arguments);
var attr = this.get('attributes');
if (attr.title)
this.set('title', attr.title);
if (attr.a_href)
this.set('title', attr.a_href);
if (attr.target)
this.set('title', attr.target);
if (attr.hoverText)
this.set('title', attr.hoverText);
if (attr.src)
this.set('title', attr.src);
},
getAttrToHTML(...args) {
var attr = defaultModel.prototype.getAttrToHTML.apply(this, args);
var src = this.get('src');
if (src)
attr.src = src;
var title = this.get('title');
if (title)
attr.title = title;
var a_href = this.get('a_href');
if (a_href)
attr.a_href = a_href;
var target = this.get('target');
if (target)
attr.target = target;
var hoverText = this.get('hoverText');
if (hoverText)
attr.hoverText = hoverText;
return attr;
}
}, {
isComponent(el) {
if ((el.getAttribute &&
el.getAttribute('data-gjs-type') == 'pcBox') || el.className === 'cust-section') {
return {
type: 'pcBox'
};
}
}
}),
// Define the View
view: defaultView.extend({
tagName: 'a',
events: {
'dblclick': 'openModal',
'click': 'initResize',
},
initialize(o) {
defaultView.prototype.initialize.apply(this, arguments);
this.listenTo(this.model, 'change:target', this.updateTarget);
this.listenTo(this.model, 'change:title', this.updateTitle);
this.listenTo(this.model, 'change:hoverText', this.updateHoverText);
this.listenTo(this.model, 'change:a_href', this.updateLink);
this.listenTo(this.model, 'active', this.openModal);
this.listenTo(this.model, 'dblclick active', this.openModal);
this.listenTo(this.model, 'change:src', this.updateSrc);
this.classEmpty = this.ppfx + 'plh-pc-box';
if (this.config.modal)
this.modal = this.config.modal;
if (this.config.am)
this.am = this.config.am;
},
updateTitle() {
$(this.el).find('h4').html(this.model.get("title"));
},
updateHoverText() {
$(this.el).find('p.img__description').html(this.model.get("hoverText"));
},
updateLink() {
$(this.el).find('a').attr('href', this.model.get("a_href"));
},
updateTarget() {
$(this.el).find('a').attr('target', this.model.get("target"));
},
updateSrc() {
var src = this.model.get("src");
$(this.el).find("img.img__img").attr('src', src);
if (!src)
$(this.el).find("img.img__img").addClass(this.classEmpty);
else
$(this.el).find("img.img__img").removeClass(this.classEmpty);
},
openModal(e) {
var em = this.opts.config.em;
var editor = em ? em.get('Editor') : '';
if (editor) {
editor.runCommand('open-assets', {
target: this.model,
onSelect() {
editor.Modal.close();
editor.AssetManager.setTarget(null);
}
});
}
},
disableClick() {
this.preventDefault();
},
render(...args) {
this.updateAttributes();
this.updateClasses();
var tempTemplate = "<div class=img__wrap><img class=img__img src=" + this.model.get("src") + "><p class=img__description>" + this.model.get("hoverText") + "</div><h4>" + this.model.get("title") + "</h4>";
var template = "<a href=" + this.model.get("a_href") + " target=" + this.model.get("target") + " onclick='event.preventDefault()'>" + tempTemplate + "</a>"
this.el.className = "pcBox";
this.el.innerHTML = (template);
return this;
}
})
});
};
`
while getting html from editor.html(), I get html with attribute added at root(div) but I need individual attribute applied at correct as per image 2
it return below html

but I need below html
Answers (3)
@krunal039 It's not an issue, it's just how you've defined a custom type. If you define traits on div you will get them there. If you need to update some nested component you can always access it from the component itself
const linkModel = model.components().at(0);
...
@artf thanks for reply, if you can please point me to any sample?
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.
Issue #1157
Return span randomly in custom component
I have custom component and on trait change wanted to updated html but randomly it returned span/textnode in trait change function insted o...
Issue #527
Model and HTML return issue
#522 @artf I tried as you said but still no luck, I do not want to waste your time but it would be very useful if you can please create sma...
Issue #1198
QUESTIONS : Find element in Model
I have custom component and it render below html, I want to update value of h4 tilte and other element on trait changed, but not sure how c...
Issue #482
Default asset manager in custom component
I am in process to create custom component for below template and we want to have user enter text,link,hover text and select image and want...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.