You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The CustomEvent interface represents events initialized by an application for any purpose. Custom events can carry arbitrary data via the Detail property. SpawnDev.SpawnJS provides both a non-generic and a generic variant for type-safe detail access.
Constructors
// Create with just a type namepublicCustomEvent(string type)// Create with type and options (including Detail data)publicCustomEvent(string type,CustomEventOptions options)// From existing referencepublicCustomEvent(SpawnJSObjectReference _ref)
Properties
Property
Type
Description
Detail
SpawnJSObject?
The custom data passed when the event was created. Returns as SpawnJSObject.
// Create and dispatchusingvarevt=newCustomEvent("userAction",newCustomEventOptions{Detail=new{Action="save",ItemId=42},Bubbles=true,Cancelable=true});element.DispatchEvent(evt);
Example - Listening for Custom Events
voidHandleCustom(CustomEvente){usingvardetail=e.Detail;if(detail!=null){varaction=detail.JSRef!.Get<string>("Action");varitemId=detail.JSRef!.Get<int>("ItemId");Console.WriteLine($"Action: {action}, Item: {itemId}");}}// Using the typed variantvoidHandleTypedCustom(CustomEvent<MyDetailType>e){vardetail=e.Detail;// Already typed as MyDetailTypeConsole.WriteLine($"Action: {detail.Action}");}
Example - Custom Event Communication Between Components
// Component A - dispatch eventusingvarevt=newCustomEvent("dataReady",newCustomEventOptions{Detail="Data is loaded",Bubbles=true});JS.CallVoid("document.dispatchEvent",evt);// Component B - listen for eventvoidOnDataReady(CustomEvente){stringmessage=e.DetailAs<string>();Console.WriteLine(message);// "Data is loaded"}// Note: Use AddEventListener on the document or a shared EventTarget