CWA 2 is a prerelease (2.0.0-alpha), heading for a beta. It runs production sites; configuration can still change between releases, always with upgrade notes. View versions and support
Admin Manager
onRemoteChange
Run your own code when another admin saves a change to a resource you're showing, with the fields that changed.
onRemoteChange calls a handler when another admin saves a change to a resource. It's for components that keep their own state, such as an editor that holds its text outside the store, and need to know that the stored copy moved under them. The module already shows the change in place (see Real-Time Updates › Co-Editing in the Admin), so most components need nothing.
Since @cwa/nuxt 2.0.0-alpha.26 (#429).
import { onRemoteChange, useCwaResourceManagerTab } from '#imports'
const { exposeMeta, iri } = useCwaResourceManagerTab({ name: 'Content' })
onRemoteChange(iri, ({ changedFields, accept }) => { if (changedFields.includes('html')) { accept() }})
defineExpose(exposeMeta)
Signature
onRemoteChange(
iri: MaybeRefOrGetter<string | undefined>,
handler: (event: RemoteChangeEvent) => void
): () => void
iri can be a string, a ref or a getter, so the handler follows the selected resource. It returns a function that stops listening. Called inside a component's setup, it stops by itself when the component unmounts.
The Event
| Property | Type | Description |
|---|---|---|
resource | CwaResource | The resource as the other admin saved it |
changedFields | string[] | Top-level properties whose value differs from the copy you had. @id, @context and _metadata are never listed |
user | { id: string } | undefined | The IRI of the user who saved it, from _metadata.lastWrite.userId. There's no name |
accept | () => void | Chooses Use theirs for every control on this resource that's waiting for a choice. It does nothing once a newer change has arrived |
When It's Called
- Only for a signed-in admin, in the browser, while co-editing is on. Visitors, the server render and an app with
cwa.realtime.enabled: falsenever call it. - Only for someone else's save. Your own saves coming back over Mercure, and a re-fetch after a reconnect that changed nothing, don't call it.
- For the draft and the live version alike: a handler on a draft's IRI hears a change to its live version, and the other way round.
- Not for a delete. The module deselects a removed component itself.
The change is already in the store when the handler runs, except for a control that's waiting for Use theirs / Keep mine: that control keeps the admin's value until they choose. Call
accept() only when your component can't offer the choice itself.