0

Normally when using js classes, I pass in any configuration data needed at instantiation, through the constructor. This can't be done with custom elements. Of course I've thought about just passing in the required data with every method call, but this gets very messy quickly. I'd rather wire everything up to expect a config object where needed, and then just call functions without input. The next obvious solution is to use a global object that each class can access and use to configure method calls on the fly. Of course, this has the huge downside of requiring a global object.

What I'm wondering is if there is a clean approach to setting up custom element code this way that I'm missing.

It would be fantastic if one could pass in arguments as part of the 3rd/"options" parameter when defining the element initially, which could then be made available in the constructor.

I guess I'll just write a factory wrapper class and roll my own constructor functionality for now...

ineedhelp
  • 227
  • 1
  • 15
  • What kind of configuration data? Can you show an example? How dynamic is the config? Is it for example an application-wide configuration stored in an object? Or is it stored somewhere on a per-user basis? – Barthy Mar 06 '20 at 10:14
  • A (pseudo)code example of what you are after would help. I use your 'global' approach.. not a global but scoped in an IIFE.. gives me a 'global' object with configuration, easily scoped to an individual element with ``Object.assign(this,$GlobalIIFEObject)`` Each element then has a method to communicate to use/update its own data or the 'global' data. So each (DOM)element has/is the API – Danny '365CSI' Engelman Mar 06 '20 at 13:22
  • If you want to go ~back~ forward to the ~future~past and not use a class see Supersharps answer: https://stackoverflow.com/questions/46071707/creating-custom-element-without-using-class-keyword – Danny '365CSI' Engelman Mar 06 '20 at 13:27
  • Why don't you instanciate you custom element with new? Then you can pass your parameters as arguments. – Supersharp Mar 06 '20 at 13:43

0 Answers0