Good way to share configuration data across multiple Lightning Components?

We are doing some work soon where multiple Lightning Components all need to access some JSON configuration data. Is there a good pattern that allows this data to be read once from the server and injected into the (fairly independent) components?

For example, one component could do the read and fire an application event containing the JSON configuration that all components then consume.

If you have any good working patterns for this situation, please share…

Answer

Static resource are cached and can be referenced in multiple places in your Lightning Component. I’m assuming you want to do use it directly in your JS controller, but it’s also possible to reference Static Resources in component markup (for instance for CSS style sheets).

Referencing Static Resources multiple times on the same page (for instance in multiple Lightning components) will result in just 1 call to the browser cache or 1 download of the static resource.

Use the $Resource value provider to reference the Static Resources in your Lightning Component.

({
    useJSONFromStaticResource: function(component) {
        var json = $A.get('$Resource.JsonStaticResource') + '/assets/jsonconfig.json';
    }
})

Attribution
Source : Link , Question Author : Keith C , Answer Author : Folkert

Leave a Comment