Understand Unicorn model and action behavior
mainUnicorn components interact with the server using specific patterns for data binding and method calls:
Models (Data Binding)
Elements with a :model attribute (or u:model) are bound to component attributes.
- Event Listeners: Unicorn attaches
changeorblurlisteners. - Lazy Modifier: Using the
lazymodifier changes the listener type. - Defer Modifier: The
defermodifier stores the action to be bundled with a subsequent action event. - Workflow: When a model event fires, a JSON payload is sent to the AJAX endpoint. The component is re-instantiated, data is updated, and the component is re-rendered.
Actions (Method Calls)
Actions are triggered via event listeners (e.g., unicorn:click).
- Argument Parsing: Arguments and keyword arguments passed from the front-end are parsed using
ast.parseandast.literal_evalto ensure Python types (like converting the string "1" to the integer1) are preserved. - Workflow: The component is re-initialized, the method is called with the parsed arguments/kwargs, and the component is re-rendered.