Grapesjs: [Question] How to get value of "traits"?

Created on 6 Jul 2017  路  12Comments  路  Source: artf/grapesjs

My code:

model: defaultModel.extend({
      init() {
        this.listenTo(this, 'change:url', this.doStuff);
      },

      doStuff() {
        console.log("change");
        console.log(this.model.value);
      },

Thanks!

outdated question

Most helpful comment

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')
    )
  })

All 12 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kawika-connell picture kawika-connell  路  3Comments

faizansaiyed picture faizansaiyed  路  3Comments

applibs picture applibs  路  3Comments

ryandeba picture ryandeba  路  3Comments

YashPrince picture YashPrince  路  3Comments