Make onExecuteAction not static in renderers (support action event handler on individual cards)
Implementation status
- [ ] TypeScript
- [x] UWP
- [ ] WPF
- [ ] iOS
- [ ] Android
- [x] .NET
- [x] Documentation
Problem
Often times, when rendering a card, that card is associated with an instance of an object. And you want the action callbacks to be associated with that object.
The problem is that, today, the Action event handler is defined on the renderer itself, meaning there's only one global action handler. For scenarios where the action invocation affects an instance of an object and needs to handle actions differently, this becomes impossible (or you need to use a separate renderer for each object).
For example, in Action Center toast notifications, there's a card associated with each toast notification. When you perform an action on the card, we need to dismiss the toast notification.
If the action invoked event handler is at the renderer level, we would somehow need to enumerate all toasts, discover which one the action invocation was related to, and then dismiss the toast.
Whereas if the action invoked event handler is on the instance of the rendered card result, the event handler already has a 1:1 relationship between the toast notification and the event handler, making it super easy to dismiss the correct toast.
Solution
Move the event handler so that it's on the rendered instance of the card.
RenderResult result = await renderer.RenderAsync(card);
result.Action += ((sender, args) => {
});
Original text
As defined here, AdaptiveCards.AdaptiveCard.onExecuteAction is a static function. This means a host app can only register one global handler - but isn't that odd? One app could be rendering cards from different sources, that need their actions handled separately.
I currently find myself doing something like this:
var _this = this;
var adaptiveCard = new AdaptiveCards.AdaptiveCard();
adaptiveCard.parse(card);
var el = adaptiveCard.render();
adaptiveCard.onExecuteAction = function(action) {
if (action instanceof AdaptiveCards.SubmitAction) {
_this.submitConfig(action.data);
}
}
AdaptiveCards.AdaptiveCard.onExecuteAction = function(action) {
action.parent.getRootElement().onExecuteAction(action);
}
But that seems backwards. Couldn't a callback just be supported on the instance?
Hi @jorenl - It never came to my mind that an app might want to process actions differently given a card's source, hence the static event handler which can be set once and for all. I could definitely make the event a property of the card, but would you mind telling me more about your particular scenario and how exactly action handling might differ on a per-card basis?
Toast notifications need this too. When an action is invoked on a specific card, we need to be able to correlate the card with the toast notification. That doesn't work if the event handler is on the renderer itself (which there's 1-many cards vs renderers)
Pseudo-code based on what Andrew is saying. Instead of 1 global handler the host could associated a callback on each renderered card.
RenderedAdaptiveCard result = await renderer.RenderAsync(card, cancellationToken: null);
result.OnAction((e) => {
});
Note this feature is available in the JS SDK:
let myCard = new AdaptiveCard();
myCard.onExecuteAction = ...;