1

Is there a convention or preferred naming convention for component attributes?

I have seen dasherized:

{{my-component initial-selection='...' on-selected=(action '...') }}

and camel cased:

{{my-component initialSelection='...' onSelected=(action '...') }}

I would have thought ember, being an opinionated framework, would enforce at least one of these naming conventions.

Daniel Kmak
  • 18,164
  • 7
  • 66
  • 89
jax
  • 37,735
  • 57
  • 182
  • 278

1 Answers1

0

Camel cased attribute naming is certainly more popular. I think it's better because attributes passed to components eventually become JavaScript properties.

Main drawback of using dasherized attributes is that you can't refer to object property which has got dash in its name using dot notation (myObject.myProperty) in JavaScript, you need to use bracket notation (myObject['my-property']).

So, if you would like to access initial-selection and rely on this.attrs, you would have to use: this.attrs['initial-selection'], while you could access initialSelection simpler: this.attrs.initialSelection.

So, if dash isn't legal in JavaScript variable names it seems more reasonable to stick to camel cased naming convention.

Daniel Kmak
  • 18,164
  • 7
  • 66
  • 89