Content Hub Tip #3: Subscribe to component events

Subscribe to component events

Once in a while, you have a requirement that you just can solve with the standard components within Content Hub. Luckily for us, Content Hub supports External components. This allows us to integrate external libraries within the Content Hub. It provides us with flexibility and allows advanced customization.

When creating such a component, you may find yourself in a bit of a pickle. How do you know when the entity on the page has been loaded, changed, etc. Well, Sitecore has come up with the use of events that you can subscribe to. 

Here are some coding examples

var entityLoadedSubscription = options.mediator.subscribe("entityLoaded", function (entity) {
    // do action
});

var entityUnloadedSubscription = options.mediator.subscribe("entityUnloaded", function (entity) {
    // do action
});

Entity events:
EventDescriptionEvent Data
entityUpdated (id)Fired when the entity (id) is updated.Entity object
entityCreatedFired when an entity is created.Entity object
entitySaved (id)Fired when the entity (id) is saved.Entity object
entityRefreshed (id)Fired when the entity (id) is refreshed.Entity object
entityChanged (id) / property (propertyName)Fired when the property (propertyName) is changed on the entity (id)Property value

Realtime events:
EventDescriptionEvent Data
realtime (notificationType)Fired when a real-time notification is receivednotification parameters
* copied from Sitecore Content Hub docs