githubEdit

Lazy Loading

A slow component can slow down the loading of an entire page. CBWIRE's lazy loading feature allows you to delay loading your components until the page is fully rendered.

Let's create a slow component that users will hate.

<!--- ./views/someview.bxm|cfm --->
<div>
    #wire( "SlowComponent" )#
</div>
// ./wires/SlowComponent.bx
class extends="cbwire.model.Component" {
    function onMount() {
        sleep( 2000 ); // Make this component slow
    }
}
<!--- ./wires/slowcomponent.bxm --->
<bx:output>
    <div>
        <h1>Slow Component</h1>
    </div>
</bx:output>

Changing this to a lazy-loaded component is as simple as using wire( lazy=true ).

#wire( name="SlowComponent", lazy=true )#

This will cause Livewire to skip this component on the initial page load. Once visible in the viewport, Livewire will request a network to load the component fully.

circle-info

Lazy loading also delays the execution of the onMount() lifecycle method.

Placeholder

As your components load, you often want to display some indicator, such as a spinner, to your users. You can do this easily using the placeholder() method.

circle-info

By default, CBWIRE inserts an empty <div></div> for your component before it loads.

circle-exclamation

Placeholder using ColdBox view

You can render a ColdBox view for a placeholder using the view() method.

Was this helpful?