[SalesForce] How to find out when Aura component / document is ready

Salesforce lightning component:

  1. I'm trying to solve an issue of accessing an object's field dynamically – e.g. obj.get(fieldAPI);

  2. After trying numerous workarounds I decided to try and Id div's uniquely and then with jQuery to populate these.
    E.g. .

  3. I have the correct code to update the div, BUT when I run the script while INIT the DOM is not ready yet [Its undefined to be accurate].

  4. Any idea how to work out these please?

Cheers!

Best Answer

If you need to wait until after the DOM is available, then you should use a renderer.

The afterRender event fires after the DOM is ready. You can then use this to assign values, modify DOM.

First you will need a function in your lightning component's helper:

{
  postRender : function(component){
    //...do some stuff here...
  }
{

Second you need to have to call this from the renderer:

({
  afterRender: function(component, helper) {
    this.superAfterRender();
    helper.postRender(component);
  }
})

Normally you don't want to use renderers for arbitrary assigning values as this is what the attribute binding is intended to accomplish. But in this case where you are dynamically assigning which fields will even be present based on object...this sounds like a good use case.

Related Topic