mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-10-25 04:54:07 +00:00
update docs
This commit is contained in:
parent
0b1e8c89dc
commit
7b73720143
586 changed files with 5616 additions and 1325 deletions
55
.cursor/rules/vide/tut/crash-course/12-actions.md
Normal file
55
.cursor/rules/vide/tut/crash-course/12-actions.md
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
# Actions
|
||||
|
||||
Actions are special callbacks that you can pass along with properties,
|
||||
to run some code on an instance receiving them.
|
||||
|
||||
```luau
|
||||
local action = vide.action
|
||||
|
||||
create "TextLabel" {
|
||||
Text = "test",
|
||||
|
||||
action(function(instance)
|
||||
print(instance.Text)
|
||||
end)
|
||||
}
|
||||
|
||||
-- will print "test"
|
||||
```
|
||||
|
||||
Actions can be wrapped with functions for reuse. Below is an example of an
|
||||
action used to listen for property changes:
|
||||
|
||||
```luau
|
||||
local action = vide.action
|
||||
local source = vide.source
|
||||
local effect = vide.effect
|
||||
local cleanup = vide.cleanup
|
||||
|
||||
local function changed(property: string, callback: (new) -> ())
|
||||
return action(function(instance)
|
||||
local connection = instance:GetPropertyChangedSignal(property):Connect(function()
|
||||
callback(instance[property])
|
||||
end)
|
||||
|
||||
-- remember to clean up the connection when the reactive scope the action
|
||||
-- is ran in is destroyed, so the instance can be garbage collected
|
||||
cleanup(connection)
|
||||
end)
|
||||
end
|
||||
|
||||
local output = source ""
|
||||
|
||||
local instance = create "TextBox" {
|
||||
changed("Text", output)
|
||||
}
|
||||
|
||||
effect(function()
|
||||
print(output())
|
||||
end)
|
||||
|
||||
instance.Text = "foo" -- "foo" will be printed by the effect
|
||||
```
|
||||
|
||||
The source `output` will be updated with the new property value any time it is
|
||||
changed externally.
|
||||
Loading…
Add table
Add a link
Reference in a new issue