# Cockpit Card Lazy Loading Reference

## Why Lazy Loading?

The cockpit home screen can display 8–12 cards simultaneously. If every card fired a DB query on open, the initial load would be O(n×queries) — slow and battery-intensive. The framework solves this with a **CREATE-then-load** pattern:

1. `EntryActions` CREATE all card LOs **empty** (no query executed)
2. UI renders instantly with empty shells
3. When a card scrolls into the visible viewport, the framework fires a `CardLoadEvent`
4. The card's action chain runs (SetDataReady → LoadItems → GetCardInformation)
5. Only then does `getTasksForCard` query the DB and populate the LO

This means a user who never scrolls to the bottom of the cockpit never pays the cost of those cards' data loading.

---

## The Pattern in Code

### EntryActions (cockpit process)

```xml
<!-- CORRECT: CREATE = empty LO, no DB hit -->
<Action name="Card<Name>_GetList" actionType="CREATE" type="Lo<Name>">
  <Return name="ProcessContext::Card<Name>_List" />
</Action>

<!-- WRONG: LOAD here defeats lazy loading entirely -->
<!-- <Action name="Card<Name>_GetList" actionType="LOAD" ... -->
```

The `actionType="CREATE"` call runs the LO's `afterCreateAsync` BL hook (if present) but does **not** trigger `loadAsync`. The LO is an empty collection in memory.

### IsReadyToLoad Binding (UI)

The `<CardContainer>` must declare this binding:

```xml
<Binding target="IsReadyToLoad" type="Text"
         binding="ProcessContext::Card<Name>_DataLoaded"
         bindingMode="ONE_WAY" />
```

`ProcessContext::Card<Name>_DataLoaded` starts as `null/false`. When the load action chain runs, `SetDataReady` writes `1` to it. The framework observes this change and marks the card as loaded.

### LoadContainerData Event (UI)

```xml
<Events>
  <LoadContainerData event="Card<Name>_loadData" />
</Events>
```

When the card enters the viewport for the first time, the framework fires the event named `Card<Name>_loadData`. This routes to the `Card<Name>_SetDataReady` action, starting the load chain.

**Both `IsReadyToLoad` and `LoadContainerData` must be present.** Missing either one produces build errors `00000016` or `00000018`.

---

## The Full Load Chain

```
User scrolls → card visible
    → LoadContainerData fires "Card<Name>_loadData"
    → Process routes to Card<Name>_SetDataReady
        → Utils.identity(1) → writes 1 to Card<Name>_DataLoaded
        → TransitionTo Card<Name>_LoadItems
    → Card<Name>_LoadItems
        → calls Lo<Name>.getTasksForCard(currentDate, numberOfListItems)
        → BL: queries DB via Facade.getListAsync(DsLo<Name>)
        → LO now has rows
        → TransitionTo Card<Name>_GetCardInformation
    → Card<Name>_GetCardInformation
        → calls Lo<Name>.getInfoForCard()
        → BL: computes "X / Y" summary string
        → returns → writes to Card<Name>_InformationText
    → UI re-renders: list rows + info header visible
```

---

## BL Implementation: getTasksForCard

The `getTasksForCard` method on the LO is where the actual DB query happens:

```javascript
/**
 * @function getTasksForCard
 * @this Lo<Name>
 * @kind listobject
 * @async
 * @param {DomDate} currentDate
 * @param {DomInteger} numberOfListItems
 */
async function getTasksForCard(currentDate, numberOfListItems) {
    var me = this;
    var jsonParams = {};
    jsonParams.currentDate = currentDate;
    jsonParams.maxListItems = numberOfListItems || 5;

    await Facade.getListAsync(me, 'DsLo<Name>', jsonParams);
}
```

This is the ONLY place `Facade.getListAsync` should be called for a cockpit card. Calling it in `EntryActions` (via `LOAD`) or in `afterCreateAsync` defeats lazy loading.

---

## Device-Aware Item Count

The `numberOfListItems` parameter comes from `ProcessContext::CardController.numberOfListItems`, which `BoSalesCockpitHelper.afterCreateAsync` sets based on device type:

-   Phone: 3 items
-   Tablet: 5 items

Pass this through from the process to `getTasksForCard` and respect it in the DS's LIMIT clause or BL filter.

---

## Anti-Patterns

| Anti-pattern                                | Effect                                    | Fix                                |
| ------------------------------------------- | ----------------------------------------- | ---------------------------------- |
| `actionType="LOAD"` in EntryActions         | All card data loaded on cockpit open      | Change to `actionType="CREATE"`    |
| `Facade.getListAsync` in `afterCreateAsync` | Data loaded on CREATE, ignores visibility | Move to `getTasksForCard`          |
| Missing `IsReadyToLoad` binding             | Build error `00000016`                    | Add the binding                    |
| Missing `LoadContainerData` event           | Card never triggers load                  | Add the self-closing event element |
| Loading all columns in the DS               | Slow card render                          | Select only columns the LI needs   |
