Rename to reactive properties

This commit is contained in:
Sabine Lim
2023-05-08 19:05:04 +10:00
parent f2e7f9d962
commit e4de37e8c2
2 changed files with 17 additions and 18 deletions

View File

@@ -8,7 +8,7 @@ local function newNode(name)
node.___name = name node.___name = name
node.___children = {} node.___children = {}
node.___props = {} node.___props = {}
node.___dynProps = {} node.___reactiveProps = {}
function node:value() return self.___value end function node:value() return self.___value end
function node:setValue(val) self.___value = val end function node:setValue(val) self.___value = val end
@@ -47,10 +47,9 @@ local function newNode(name)
table.insert(self.___props, { name = name, value = self[lName] }) table.insert(self.___props, { name = name, value = self[lName] })
end end
function node:dynamicProperties() return self.___dynProps end function node:reactiveProperties() return self.___reactiveProps end
function node:numDynamicProperties() return #self.___dynProps end function node:addReactiveProperty(name, value)
function node:addDynamicProperty(name, value) self.___reactiveProps[name] = value
self.___dynProps[name] = value
end end
return node return node
@@ -87,15 +86,15 @@ function XmlParser:FromXmlString(value)
return value; return value;
end end
function XmlParser:ParseArgs(node, s) function XmlParser:ParseProps(node, s)
string.gsub(s, "(%w+)=([\"'])(.-)%2", function(w, _, a) string.gsub(s, "(%w+)=([\"'])(.-)%2", function(w, _, a)
node:addProperty(w, self:FromXmlString(a)) node:addProperty(w, self:FromXmlString(a))
end) end)
end end
function XmlParser:ParseDynamicArgs(node, s) function XmlParser:ParseReactiveProps(node, s)
string.gsub(s, "(%w+)={(.-)}", function(w, a) string.gsub(s, "(%w+)={(.-)}", function(w, a)
node:addDynamicProperty(w, a) node:addReactiveProperty(w, a)
end) end)
end end
@@ -115,13 +114,13 @@ function XmlParser:ParseXmlText(xmlText)
end end
if empty == "/" then -- empty element tag if empty == "/" then -- empty element tag
local lNode = newNode(label) local lNode = newNode(label)
self:ParseArgs(lNode, xarg) self:ParseProps(lNode, xarg)
self:ParseDynamicArgs(lNode, xarg) self:ParseReactiveProps(lNode, xarg)
top:addChild(lNode) top:addChild(lNode)
elseif c == "" then -- start tag elseif c == "" then -- start tag
local lNode = newNode(label) local lNode = newNode(label)
self:ParseArgs(lNode, xarg) self:ParseProps(lNode, xarg)
self:ParseDynamicArgs(lNode, xarg) self:ParseReactiveProps(lNode, xarg)
table.insert(stack, lNode) table.insert(stack, lNode)
top = lNode top = lNode
else -- end tag else -- end tag
@@ -205,7 +204,7 @@ return {
setValuesByXMLData = function(self, data, scripts) setValuesByXMLData = function(self, data, scripts)
scripts.env[self:getName()] = self scripts.env[self:getName()] = self
for k,v in pairs(data:dynamicProperties()) do for k,v in pairs(data:reactiveProperties()) do
local sharedVariable = string.sub(v, 8, -1) local sharedVariable = string.sub(v, 8, -1)
local sharedObservers = scripts.env.sharedObservers local sharedObservers = scripts.env.sharedObservers
if (sharedObservers[sharedVariable]) == nil then if (sharedObservers[sharedVariable]) == nil then

View File

@@ -82,9 +82,9 @@ Here's an example of using the shared table to share data between two `<script>`
In this example, we first store a value in the shared table in one `<script>` tag, and then access that value in another `<script>` tag to update the titleLabel's text. In this example, we first store a value in the shared table in one `<script>` tag, and then access that value in another `<script>` tag to update the titleLabel's text.
You can also include Lua code directly within event attributes of UI elements. This allows you to execute specific actions or manipulate UI elements when certain events occur. You can also include Lua code directly within event properties of UI elements. This allows you to execute specific actions or manipulate UI elements when certain events occur.
To include Lua code in an event attribute, simply add the Lua code within the event attribute's value in the XML tag. The Lua code will be executed when the specified event is triggered. To include Lua code in an event property, simply add the Lua code within the event property's value in the XML tag. The Lua code will be executed when the specified event is triggered.
```xml ```xml
<label id="titleLabel" text="Welcome to Basalt!" x="10" y="2" /> <label id="titleLabel" text="Welcome to Basalt!" x="10" y="2" />
@@ -108,11 +108,11 @@ Notably, you can access UI elements by their assigned ID directly in the event c
Remember: IDs have to be unique! Remember: IDs have to be unique!
## Dynamic attributes (BETA) ## Reactive properties (BETA)
Most attributes can also be set to track a shared variable using the curly braces {} syntax. In this case, the initial value for the variable should be set inside the `<script>` tag. Most properties can also be set to track a shared variable using the curly braces {} syntax. In this case, the initial value for the variable should be set inside the `<script>` tag. When this variable is modified, the rendered value will be automatically updated.
The earlier example rewritten using dynamic attributes: The earlier example rewritten using reactive properties:
```xml ```xml
<label id="titleLabel" text="Welcome to Basalt!" x="10" y="2" /> <label id="titleLabel" text="Welcome to Basalt!" x="10" y="2" />