mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
Update docs
This commit is contained in:
parent
f2de9b0e63
commit
62f1c3a20a
16 changed files with 86 additions and 347 deletions
53
docs/tut/crash-course/12-actions.md
Normal file
53
docs/tut/crash-course/12-actions.md
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
# Actions
|
||||
|
||||
Actions in Vide are special callbacks that you can pass along with properties,
|
||||
to run some code on an instance receiving them.
|
||||
|
||||
```lua
|
||||
local action = vide.action
|
||||
```
|
||||
|
||||
```lua
|
||||
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:
|
||||
|
||||
```lua
|
||||
local action = vide.action
|
||||
local cleanup = vide.cleanup
|
||||
|
||||
local function changed(prop: string, callback: (new) -> ())
|
||||
return action(function(instance)
|
||||
local connection = instance:GetPropertyChangedSignal(prop):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)
|
||||
}
|
||||
|
||||
instance.Text = "foo"
|
||||
|
||||
print(output()) -- "foo"
|
||||
```
|
||||
|
||||
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