Recipes
Expanded rows survive a list refresh
blade
<section id="orders" @renderObject('orders', ['render', 'state:read', 'state:write'])>
@foreach ($orders as $o)
<button type="button" data-state-toggle="open{{ $o->id }}">#{{ $o->id }}</button>
<div data-state-show="open{{ $o->id }}">…details…</div>
@endforeach
<button type="button" @renderBlade(OrderController::class, 'components', [], '#orders')>Refresh</button>
</section>The list is replaced wholesale, and the rows that were expanded stay expanded.
Forms: no lost input when a failed validation re-renders
blade
<form @renderObject('customerForm', ['render', 'state:read', 'state:write'])>
<input name="name" data-state-bind="name" value="{{ $customer->name }}">
<input name="email" data-state-bind="email" value="{{ $customer->email }}">
</form>On the successful-save path, clear it; otherwise old input overwrites the new values from the server:
php
return $this->ok()
->call('ui.toast', ['level' => 'success', 'message' => 'Saved'])
->then('state.clear', ['scope' => 'customerForm'])
->then('render.fragment', ['target' => '#customer-form', 'name' => CustomerController::class, 'fresh' => true]);Dependent fields
blade
<select name="method" data-state-bind="method">
<option value="pickup">Pickup</option>
<option value="delivery">Delivery</option>
</select>
<div data-state-is="method=delivery">…address fields…</div>For a simple comparison, use data-state-is. For a real derived value (say, based on an amount), compute it in a CGEvent and set it, or have Linker return it.
Sharing a value across objects
blade
<span data-state-text="@cartCount">0</span>The @ prefix uses a global key; permission still comes from the enclosing object. Use sparingly, and name it so it's obviously shared.
Two calculators on one page
blade
<section @renderObject('calcA', [...])>@include('partials.calculator')</section>
<section @renderObject('calcB', [...])>@include('partials.calculator')</section>Live example: /calculator-demo.
Code review checklist
- [ ] Every block using
data-state-*or renderBlade is inside an@renderObject - [ ] The allow list only grants actions that are used
- [ ]
paramssent to the server are picked explicitly, not the whole state - [ ] The server validates every param itself
- [ ] The successful-save path clears state
- [ ] Hand-written
subscribe()calls releaseoff() - [ ] No passwords, files or amounts in state