My code:
model: defaultModel.extend({
init() {
this.listenTo(this, 'change:url', this.doStuff);
},
doStuff() {
console.log("change");
console.log(this.model.value);
},
Thanks!
doStuff() {
this.get('traits').each(function(trait){
console.log(trait.get('value'));
});
},
Works like butter...!!!
Thanks!
How about getting particular named trait?:innocent:
this should work:
this.get('traits').where({name: '...'})[0].get('value')
Uncaught TypeError: this.get is not a function
Check if the model is actually returned this.get('traits').where({name: '...'})[0], you might passed a wrong name
I checked all. Didn't worked. Same error. But I got it working for now by pushing values into array.
Thanks for the help.
How to get the value of a particular or single trait? @artf
@HarshOB just like above
componentModel.get('traits').where({name: 'TRAIT-NAME'})[0].get('value')
And if you don't have selected component context, here's how I get all values of a type:
window.grapesjs.editors[0]
.getComponents()
.models.filter(_ => _.attributes.type === 'facebook-messenger')
.forEach(_ => {
console.log(
_.get('traits')
.where({
name: 'campaign',
})[0]
.get('value')
)
})
And if you don't have selected component context, here's how I get all values of a type:
window.grapesjs.editors[0] .getComponents() .models.filter(_ => _.attributes.type === 'facebook-messenger') .forEach(_ => { console.log( _.get('traits') .where({ name: 'campaign', })[0] .get('value') ) })
One year later, I cannot seem to get this to work.
@artf, how do we get and set traits of a specific type?
Or even better, how can I set up a trait so that I can select it by ID rather than the default .gjs-trt-trait class and/or its children?
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.
Most helpful comment
And if you don't have selected component context, here's how I get all values of a type: