Model and HTML return issue
Question
#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 small sample or point me to sample, I am sure I am missing something. sorry had to open another issue but it in continuation of same #522,
here it editor.html() only return default value when component render any of change in trait is not returned.
I can see updated values in canvas but when I get html via editor.html() gives default values but not updated via trait.
`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,
type: 'pcBox',
tagName:'div',
void: 0,
src: '/sites/testUniTest/Images1/Environment/level3/balloons_over_turkey.png',
hoverText: 'Link to curriculum page',
title: ' Curriculum Link',
a_href: 'https://team.global.test/sites/testUni',
target: '_blank',
attributes: {
frameborder: 0,
'data-gjs-type': 'pcBox',
},
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,
},
],
}),
init(){
},
initialize(o) {
defaultModel.prototype.initialize.apply(this, arguments);
this.listenTo(this, 'change:target', this.updateTarget);
this.listenTo(this, 'change:title', this.updateTitle);
this.listenTo(this, 'change:hoverText', this.updateHoverText);
this.listenTo(this, 'change:a_href', this.updateLink);
this.listenTo(this, 'active', this.openModal);
this.listenTo(this, 'dblclick active', this.openModal);
this.listenTo(this, '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;
this.class = this.class && this.class.indexOf("pcBox") > -1? this.class : this.class+ " pcBox";
},
updateAll(){
this.updateTitle();
this.updateHoverText();
this.updateLink();
this.updateTarget();
this.updateSrc();
},
updateTitle() {
var linkModel = this.get("components").at(0);
if (linkModel) {
var h2Model = linkModel.get("components").at(1);
var title = this.get('title');
if (title)
h2Model.content = title;
}
},
updateHoverText() {
var linkModel = this.get("components").at(0);
if (linkModel) {
var imageModel = linkModel.get("components").at(0);
var hoverTextModel = imageModel.get("components").at(1);
var hoverText = this.get('hoverText');
if (hoverText)
hoverTextModel.content = hoverText;
}
},
updateLink() {
var linkModel = this.get("components").at(0);
if (linkModel) {
var a_href = this.get('a_href');
if (a_href)
linkModel.href = a_href;
}
},
updateTarget() {
var linkModel = this.get("components").at(0);
if (linkModel) {
var target = this.get('target');
if (target)
linkModel.target = target;
}
},
updateSrc() {
var linkModel = this.get("components").at(0);
if (linkModel) {
var imageModel = linkModel.get("components").at(0);
var imageTagModel = imageModel.get("components").at(0);
var src = this.get('src');
if (src)
imageTagModel.src = src;
}
}
}, {
isComponent(el) {
if ((el.getAttribute &&
el.getAttribute('data-gjs-type') == 'pcBox') || el.className === 'gjs-cust-pcBox pcBox') {
return {
type: 'pcBox'
};
}
}
}),
// Define the View
view: defaultView.extend({
tagName: 'div',
events: {
'dblclick': 'openModal',
'click': 'openSettings',
},
openSettings(e) {
var openBlocksBtn = editor.Panels.getButton('views', 'open-blocks');
openBlocksBtn && openBlocksBtn.set('active', 0);
var openTMBtn = editor.Panels.getButton('views', 'open-tm');
openTMBtn && openTMBtn.set('active', 1);
},
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);
}
});
}
this.$el.click();
},
disableClick() {
this.preventDefault();
},
render(...args) {
this.updateClasses();
this.updateAttributes();
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 = "gjs-cust-pcBox pcBox";
this.el.setAttribute("data-gjs-type", "pcBox");
this.el.innerHTML = (template);
this.model.updateAll();
return this;
}
})
});
};`Answers (2)
@krunal039 this isn't a support form. If you have any problems asking here is only going to infuriate the contributors. If you have a problem ask on Stack overflow, thats what its for....
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 #522
Model Html Issue
#513 Sorry for opening new issue but you have closed last one so had to create new one, I am struggling with below so if you can help to re...
Issue #513
Model and returned html issue
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 mis...
Issue #459
Error when including a custom component inside another block
Sorry for the vague title. I'm not sure how to describe this concisely. I have a custom block where i want to restrict that only images can...
Issue #528
Model and Component sample code and issue
@artf Sorry for creating another issue but I tried below code also I have created codepen for same. Codepen and still it only return empty...
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.