Module Binding
Adds listeners or bindings to objects.
Usage:
-- Add a listener to print whenever a value is changed.
local sometable = { a = "somevalue" }
sometable = Binding.proxy(sometable)
sometable:addListener("a", function(t, k, old, new)
print("Key " .. k .. " changed from " .. old .. " to " .. new)
end
-- Bind a value in a table to the sum of two other values.
local table1 = Binding.proxy({ a = 4 })
local table2 = Binding.proxy({ a = 5 })
local sumtable = Binding.proxy({})
sumtable:bind("sum", Binding(table1, "a"):add(Binding(table2, "a")))
-- sumtable.sum == 9
table1.a = 6
-- sumtable.sum == 11
Functions
-
bind (target, key, value)
-
Binds the key in the specified table to the given value
Parameters:
- target
The table where the key to be bound is.
- key
The key to be bound.
- value
The value to bind to.
-
bindBidirectional (t1, k1, t2, k2)
-
Binds the key in the specified table to the key in the other table, and
vice versa.
Parameters:
- t1
The first table where the key to be bound is.
- k1
The key in the first table to be bound.
- t2
The second table where the key to be bound is.
- k2
The key in the second table to be bound.
-
isValue (object)
-
Whether the table is a Binding or not.
Parameters:
- object
The table to check.
Returns:
True if the table is a binding, false if not.
-
proxy (instance)
-
Returns a proxy to a table that allows listeners and bindings to be attached.
Parameters:
- instance
The table to proxy.
Returns:
A proxy table to the given instance.
-
unbind (target, key)
-
Removes the binding on the given key in the given target.
Parameters:
- target
The table to remove the binding from.
- key
The key to unbind.
-
value (t, k)
-
Creates a binding bound to the given key in the given table.
Parameters:
- t
The table the binding is bound to.
- k
The key the binding is bound to.
Returns:
A binding to the key in the given table.
Class Binding
Contains a value that is based off whatever this binding is bound to.
-
Binding:AND (...)
-
Creates a new binding containing this binding AND others.
Parameters:
- ...
Can be bindings, or constants (number, etc.)
Returns:
A new binding.
-
Binding:NOT ()
-
Creates a new binding containing NOT this binding.
Returns:
A new binding.
-
Binding:OR (...)
-
Creates a new binding containing this binding OR others.
Parameters:
- ...
Can be bindings, or constants (number, etc.)
Returns:
A new binding.
-
Binding:THEN (ifTrue, ifFalse)
-
Creates a new binding with the value of the first value if this binding is
true, or the second value if this binding is false.
Parameters:
- ifTrue
The value the new binding will be set to when this value is
true. Can either be another value, or a constant (number, etc.)
- ifFalse
The value the new binding will be set to when this value is
false. Can either be another value, or a constant (number, etc.)
Returns:
A new binding.
-
Binding:add (...)
-
Creates a new binding containing the sum of this binding and others.
Parameters:
- ...
Can be bindings, or constants (number, etc.)
Returns:
A new binding.
-
Binding:addValueBinding (binding)
-
Convenience method for adding a binding to "value"
Parameters:
- binding
The binding to add.
Returns:
The added binding.
-
Binding:addValueListener (listener)
-
Adds a listener to the value of this binding.
Parameters:
- listener
The listener to add.
Returns:
The added listener.
-
Binding:concat (...)
-
Creates a new binding containing the concatenation of this binding and
others.
Parameters:
- ...
Can be bindings, or constants (number, etc.)
Returns:
A new binding.
-
Binding:div (...)
-
Creates a new binding containing the quotient of this binding and others.
Parameters:
- ...
Can be bindings, or constants (number, etc.)
Returns:
A new binding.
-
Binding:eq (other)
-
Creates a new binding representing if this value is equal to another.
Parameters:
- other
Can be another binding or a constant (number, etc.)
Returns:
A new binding.
-
Binding:ge (other)
-
Creates a new binding representing if this value is greater than or equal
to another.
Parameters:
- other
Can be another binding or a constant (number, etc.)
Returns:
A new binding.
-
Binding:gt (other)
-
Creates a new binding representing if this value is greater than to another.
Parameters:
- other
Can be another binding or a constant (number, etc.)
Returns:
A new binding.
-
Binding:le (other)
-
Creates a new binding representing if this value is less than or equal
to another.
Parameters:
- other
Can be another binding or a constant (number, etc.)
Returns:
A new binding.
-
Binding:len ()
-
Creates a new binding containing the length of this binding.
Returns:
A new binding.
-
Binding:lt (other)
-
Creates a new binding representing if this value is less than to another.
Parameters:
- other
Can be another binding or a constant (number, etc.)
Returns:
A new binding.
-
Binding:mod (...)
-
Creates a new binding containing the modulus of this binding and others.
Parameters:
- ...
Can be bindings, or constants (number, etc.)
Returns:
A new binding.
-
Binding:mul (...)
-
Creates a new binding containing the product of this binding and others.
Parameters:
- ...
Can be bindings, or constants (number, etc.)
Returns:
A new binding.
-
Binding:ne (other)
-
Creates a new binding representing if this value is not equal to another.
Parameters:
- other
Can be another binding or a constant (number, etc.)
Returns:
A new binding.
-
Binding:negate ()
-
Creates a new binding containing the negation of this binding.
Returns:
A new binding.
-
Binding:pow (...)
-
Creates a new binding containing the exponentiation of this binding and
others.
Parameters:
- ...
Can be bindings, or constants (number, etc.)
Returns:
A new binding.
-
Binding:removeValueBinding (binding)
-
Convenience method for removing a binding from "value"
Parameters:
- binding
The binding to remove.
Returns:
Whether a binding was removed.
-
Binding:removeValueListener (listener)
-
Removes a listener to the value of this binding.
Parameters:
- listener
The listener to remove.
Returns:
Whether a listener was removed.
-
Binding:sub (...)
-
Creates a new binding containing the difference of this binding and others.
Parameters:
- ...
Can be bindings, or constants (number, etc.)
Returns:
A new binding.
-
Binding:tonumber ()
-
Creates a binding containing the number value of this binding.
Returns:
A new binding.
-
Binding:tostring ()
-
Creates a binding containing the string representation of this binding.
Returns:
A new binding.
-
Binding:unbind ()
-
Unbinds this binding, as well as anything bound to it.
Class Proxy
A proxy to allow listeners & bindings to be attached.
-
Proxy:addBinding (key, binding)
-
Adds a binding to the specified key in this table.
Parameters:
- key
The key to bind to.
- binding
The binding to attach.
Returns:
The added binding.
-
Proxy:addListener (key, listener)
-
Adds a listener to the specified key that is called when the key's value
changes.
Parameters:
- key
The key to track changes to
- listener
The function to call upon the value of the key changing.
The function should have the arguments (t, k, old, new) where:
t is the table in which the change happened.
k is the key whose value changed.
old is the old value of the key.
new is the new value of the key.
If the function changes the key's value, it should return the new value.
Returns:
The added listener.
-
Proxy:removeBinding (key, binding)
-
Removes a binding from a key in this table.
Parameters:
- key
The key to remove a binding from.
- binding
The binding to remove.
Returns:
Whether a binding was removed.
-
Proxy:removeListener (key, listener)
-
Removes the first instance of the given listener from the given key.
Parameters:
- key
The key the listener is attached to.
- listener
The listener to remove.
Returns:
Whether a listener was removed.