4

I have built a reusable vertical navigation component. I plan on using this component across multiple pages and don't know where I am supposed to put the initialization code, which will set the menu headers and items. Obviously, if I do this in the component controller itself, it will be the same across all pages I use it on, so I need a way of setting page-specific initialization attribute values.

In traditional web app development, one could pass the initialization data through a page-level controller model, but how does one accomplish the same in a autonomous Lightning Component, such as the navigation component I referenced above?

I need to be able to set the items values differently, depending on the page the component is added to. It seems that I need to set the values externally from the component itself.

2 Answers 2

3

You should use design attributes for your component bundle

This way you can set different attributes for each component instance - and this is for app builder and for community builder

3
  • Thanks for the answer. Yes, I thought of that, but since the attribute is Object[], this is not possible. Ergo, I am looking for a way to initialize the component on each page with different initialization Object[] values. Feb 6, 2018 at 15:52
  • So put it as a string and stringify it when you put it in the community builder, then on your component code - in the init JSON.parse this string attribute Feb 7, 2018 at 6:42
  • Thanks Itai, this is ultimately what I ended up doing. Appreciate you help. Feb 7, 2018 at 16:55
2

Simply assign the value you want to use in the parent component to the items attribute of your component, and the framework will render your menu appropriately.


Any initialization you may need to do internally can be done using aura:valueInit:

<aura:handler name="init"
                      value="{!this}"
                      action="{!c.doInit}"/>

And define it in your controller:

({
    doInit: function(component, event, helper) {
        component.set("v.items", ...)
    }
})
4
  • Thanks. What if the parent component is a Communities page? How do I assume attributes from there? Feb 6, 2018 at 0:13
  • @SwisherSweet see edit. I'd this what you're looking for?
    – sfdcfox
    Feb 6, 2018 at 0:28
  • If I'm setting the items values inside the component itself, then this unfortunately not what I'm looking for. I need to be able to set the items values differently, depending on the page the component is added to. It seems that I need to set the values externally from the component itself. Feb 6, 2018 at 16:06
  • 1
    @SwisherSweet You'd have to use a design attribute, as Itai mentioned. Since we only get native primitives basically, you might want to just do some post-processing in your init function.
    – sfdcfox
    Feb 6, 2018 at 17:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .