Visual Scripting
Visual Script
Events
Store
Implement interactive logic: click events and dynamic creation of cards and tasks.
Getting Started with a real app (Episode 4)
Back to the Academy
Visual Scripting
Express Summary: Implement interactive logic with visual scripting: click events, store management, and dynamic creation of cards and tasks.
Introduction to Visual Scripting
We now have a nice interface, but it's not interactive. It's time to add logic to be able to create cards and tasks using visual scripting.
1. Create a click event
Add event to "Add a task" button
- Go to the Card component
- Select the add task button div
- In the right panel, click Events then "+"
- Choose the Click event
- Rename the div to "add card" for clarity
Access the logic
- Click on the event or go to the Logic tab
- The event appears as "onclick (add card)"
2. Understand the visual scripting system
Block types and connections
- Execution blocks: white wire that defines execution order
- Data blocks: colored wires by type
- Yellow: string
- Green: number
- Red: boolean
- Blue: object
- Square: array of corresponding type
Test example
- Create a Log block
- Connect it to the onclick event with the white wire
- Add text: "hello"
- Test with right-click � Execute to see animation
- Preview: clicking "add card" displays "hello" in console
3. Retrieve and manipulate properties
Access component properties
- Right-click � type "get"
- Select "get tasks" or "get title"
- These blocks retrieve properties defined in the component
Add a task with Push
- Use the Push block to add an element to an array
- Connect get tasks to the array input of Push block
- In "value", write "new task"
- Connect onclick to the Push block
- Test: each click adds a new task
4. Create the card addition system
Modify CreateButton component
The button should have two states:
- Normal state: "Add a card" button
- Edit state: text input + validation/cancel buttons
Create state variable
- Add an isEditing variable (boolean) in CreateButton
- Wrap the button in a Template
- Condition: display if
isEditing = false
Create edit interface
- Add a second Template with condition
isEditing = true - Insert a Text Input from UI Library
- Add two icons: validation (check) and cancel (cross)
- Style with display flex and align-items center
5. State switching logic
Switch to edit mode
- Add a click event on the "Add a card" button
- In Logic, use Set isEditing to
true
Handle text input
- Create a title variable (string) in CreateButton
- On Text Input, set v-model =
title - Display icons only if
titleempty` (Template with condition)
6. Validation and cancellation
Validation event (check)
- Add a click on the validation icon
- Retrieve value with get title
- Test with a Log to verify retrieval
Cancel event (cross)
- Add a click on the cancel icon
- Use Set title with empty string
- Field empties automatically thanks to v-model
7. Create a card in the store
Understand data structure
A card contains:
- title: string
- tasks: array of strings
Retrieve store data
- Use get store to access global data
- Use spread to decompose the object and retrieve the card list
- Spread allows extracting object properties
Add the new card
- Use Push on the store's card list
- Create a Create Object of type CardType
- Connect get title to the object's title field
- Leave tasks as empty array
- Connect everything to the validation event
Reset interface
After card creation:
- Set title to empty
- Set isEditing to
false
Advanced concepts
The spread operator
- Allows decomposing an object to access its properties
- Example:
store.cardsbecomes accessible via store spread - Can be chained to access nested properties
Useful shortcuts
- Ctrl + Space: see the value of each element
- Ctrl + Alt: see the expected type for each connection
- Right-click + Execute: test execution with animation
Final result
The application now allows:
- Adding tasks to existing cards
- Creating new cards with custom titles
- All data is saved in the store
Coming next: Computed variables for a smooth and pleasant user experience.