|
| 1 | +# ⚡ Event |
| 2 | + |
| 3 | +An event defines an application event/message. |
| 4 | +An event can be defined with a handler or just defined (without a handler). |
| 5 | + |
| 6 | +## Definition |
| 7 | + |
| 8 | +1. Define the event data object or specify which of the already defined descendants ⚡ [IEvent][IEvent] is suitable for your event: |
| 9 | + |
| 10 | +```javascript |
| 11 | +class ClickHandlerRegister extends IEvent { |
| 12 | + constructor() { |
| 13 | + super(); |
| 14 | + this.handlerId = undefined; |
| 15 | + this.handler = undefined; |
| 16 | + } |
| 17 | +} |
| 18 | +``` |
| 19 | + |
| 20 | +2. Define the plugin class for the event: |
| 21 | + |
| 22 | +```javascript |
| 23 | +class pEventPlugin extends IPlugin { |
| 24 | + static EVT_CLICK_HANDLER_REGISTER = 'ClickHandlerRegister'; |
| 25 | + |
| 26 | + init() { |
| 27 | + const T = this.constructor; |
| 28 | + const TI = this; |
| 29 | + |
| 30 | + // (B) |
| 31 | + |
| 32 | + super.init(); |
| 33 | + } |
| 34 | +} |
| 35 | +Plugins.catalogize(pEventPlugin); |
| 36 | +``` |
| 37 | + |
| 38 | +3. Decide whether you can have the handler directly in the plugin or whether you need to have it outside the plugin. Depending on your case, choose the necessary procedure according to the following chapters. |
| 39 | + |
| 40 | +4. Always insert the source code of the chapter into the comment **(B)**. |
| 41 | + |
| 42 | +### ⚡ Event with handler |
| 43 | + |
| 44 | +```javascript |
| 45 | + const h_EVT_CLICK_HANDLER_REGISTER = (reply) => { |
| 46 | + // ... |
| 47 | + reply.result = 'replyValue'; |
| 48 | + } |
| 49 | + TI.eventDefinitions.push([T.EVT_CLICK_HANDLER_REGISTER, ClickHandlerRegister, h_EVT_CLICK_HANDLER_REGISTER]); |
| 50 | +``` |
| 51 | + |
| 52 | +### 📄⚡ Event only defined |
| 53 | + |
| 54 | +```javascript |
| 55 | + TI.eventDefinitions.push([T.EVT_CLICK_HANDLER_REGISTER, ClickHandlerRegister, null]); // outside event handlers |
| 56 | +``` |
| 57 | + |
| 58 | +### Meaning of elements |
| 59 | + |
| 60 | +- class ClickHandlerRegister - event data object (it is possible to use the same class for multiple events or inherit from any IEvent descendant) |
| 61 | +- ClickHandlerRegister.constructor - contains the default values of the message object. It is desirable that the values be pre-filled with the default value according to the planned type, if possible. This data is used in the [object explorer][oexplorer] as part of the system's automatic documentation. |
| 62 | +- const h_EVT_CLICK_HANDLER_REGISTER - event handler |
| 63 | +- reply.result - standard event property (⚡ [IEvent][IEvent]), which is automatically taken over by the application logic as the handler's response to the event. It can be any object. |
| 64 | +- TI.eventDefinitions.push([T.EVT_CLICK_HANDLER_REGISTER, ClickHandlerRegister, null]) - creates an event definition prescription (event name, event data object, and handler method, or null if the event is only to be defined). The processing of definitions is handled by the ancestor 🔌 [IPlugin][IPlugin]. |
| 65 | +- super.init(); - performs standard initialization of bindings. It must be the last command in order for everything defined to be initialized properly. |
| 66 | + |
| 67 | +## Sending an event |
| 68 | + |
| 69 | +In the handler code, you can then trigger events as follows: |
| 70 | + |
| 71 | +```javascript |
| 72 | + const eventName = EventNames.MyNewEvent; //nebo: 'MyNewEvent' |
| 73 | + |
| 74 | + sendEvent(eventName, (r) => { |
| 75 | + r.id = aliasName; |
| 76 | + r.result = loadedCount; |
| 77 | + //vyplnění dalších potřebných dat |
| 78 | + }); |
| 79 | +``` |
| 80 | + |
| 81 | +In the **appmainBaseLogic.js** file, you will find examples of functions that encapsulate similar calls. |
| 82 | +⚠️ It is recommended to create a similar set of functions for your plugin in a separate file (which you will add to the [list of scripts][jsList]). Calls to the **sendEvent** method are not indexed for the [object explorer][oexplorer], but these functions will be indexed. |
| 83 | + |
| 84 | +## Records 🛰️ |
| 85 | + |
| 86 | +A record in the 🛰️ [object explorer][oexplorer] means that: |
| 87 | + |
| 88 | +- in the plugin instance overview: the plugin sends the event ... |
| 89 | +- in the list of event communication paths: the event is sent from ... |
| 90 | + |
| 91 | +[IEvent]: :_evt:IEvent.md "IEvent" |
| 92 | +[oexplorer]: oexplorer.md "Object explorer" |
| 93 | +[IPlugin]: :_plg:IPlugin.md "IPlugin" |
| 94 | +[jsList]: js.lst.md "List of scripts" |
0 commit comments