Possible workaround for local constants in behaviors
Bob Sneidar
bobsneidar at iotecdigital.com
Fri Sep 22 16:40:31 EDT 2017
Hi all.
I came up with a workaround for local constants in behaviors. It doesn't actually use constants. Instead I have an array of "constants" I set for a particular object. For example, in my customer stack properties, I have an array with key value pairs like so:
cParentTable1:
cParentKey3:
cDGName: dgCustomers
cParentKey1:
cTableName: Customers
cParentTable3:
cAltkey1: contactid
cPriKey: custid
cUniqueValueList: customername
cFocusField: fldCustomerName
cParentTable2:
cAltDGName1: dgcontacts
I don't really need the empty values, but this is a standard array in all my stacks, and the empty values aren't used.
Now in my behavior for a New button I have the following. Note that the local variables have the same names as the array keys:
local cTableName, cDGName, cFocusField -- note this is only a subset of the array keys
on getStackConstants
put getParentStack(the long id of me) into tThisStack -- just gets the long name of the parent stack
-- retrieve stack constants from stack property
put the stackConstants of tThisStack into aStackConstants -- retrieves the constants array for the parent stack
put the keys of aStackConstants into tConstantList
repeat for each line tConstant in tConstantList
do "put " & quote & aStackConstants [tConstant] & quote & " into " & tConstant -- variable of the same name as the array key
end repeat
end getStackConstants
Then I have a mouseUp handler in the behavior script that is the actual behavior for a New Customer button, but of course it could be any handler. Calling getStackConstants first, I preload the local variables with the values from the array. Of course they do not have to be script locals, but then you would have to call getStackConstants in every handler in the behavior. This way you only need to call it in the initial handler:
on mouseUp pMouseBtnNo
-- load stack constants
getStackConstants -- create behavior local variables
-- whatever code here. I snipped mine for brevity
end mouseUp
The upshot is, now in each stack that has a btnNew button with this behavior set and an array with the "constants" set to what I need, everything just works. Of course, the array could be a property of any object, and the command could be getConstants pLongObjectID or something. In fact I will probably do that. The trick is that getStackConstants needs to live in the behavior script.
Hope this helps others to consider using behaviors when they need some kind of constants or set of fixed variables.
Bob S
More information about the use-livecode
mailing list