behaviors
Behaviors are stacks (binary or script-only) that add features to other stacks or objects, but only if they are specifically used by that other stack or object. Unlike libraries, behaviors are not inserted into the message path for your application.
An example of a behavior is a stack that causes a button to flash when you click on it. Only the buttons that use the behavior will flash when clicked.
The behaviors
folder in your app folder is where you store behavior stacks for your application. Behavior stacks can be script-only stacks or binary stacks.
You can chain behaviors together in Levure - one behavior can use another behavior which can use another behavior, etc. The LoadBehavior
is dispatched after all behaviors in the behaviors
folder and all helpers have been loaded.
Note: Script-only behavior stacks tied to specific objects in your user interface (UI) stacks do not go into this behaviors
folder. They will go into behaviors
folders within the UI stack folders in the ui
folder, as documented here.
- Adding behaviors to your application
- The behaviors section in app.yml
- Assigning a behavior
- Using loadBehavior to chain behaviors
To add a behavior to your application simply add the stack or script-only stack to the behaviors
folder. The next time you load your application in the LiveCode IDE all stacks in the behaviors
folder will be loaded into memory and available globally for use as behaviors.
- 📂 app
- 📂 behaviors
- my_behavior.livecodescript
- 📂 behaviors
If you add a subfolder to the behaviors
folder then all stacks and script-only stacks in the subfolder will also be loaded into memory when you open your application.
The behaviors
section in your app.yml
file specifies which stacks will be loaded as behaviors. By default, app.yml
is configured to load all stacks in the behaviors
folder and its subfolders:
# app.yml
behaviors:
- folder: ./behaviors
If you wish, you can customize the behaviors
section to load stacks from other folders or to override the default encryption setting for some of your stacks.
TODO: Give app.yml
examples of:
-
Loading a shared behaviors folder
-
Loading an individual shared behavior stack
behaviors: - filename: ../../shared/my_behavior.livecodescript - folder: ./behaviors
-
Overriding encryption for a behaviors folder or subfolder
-
Overriding encryption for an individual behavior stack
behaviors: - filename: ./behaviors/my_unencrypted_behavior.livecodescript encrypt: false - folder: ./behaviors
# app.yml
behaviors:
- filename: [relative path to stack file within behaviors folder]
encrypt: true|false [optional parameter that can override the `encrypt stacks` setting for this stack]
- folder: [relative path to a folder containing behavior stack files]
encrypt: true|false [optional parameter that can override the `encrypt stacks` setting for all stacks in the folder]
Assign a behavior using the with behavior
keyword on the script
line of a script-only stack.
In your behaviors folder you have a stack flash.livecodescript:
script "flash"
on flash
doSomething
end flash
Assume that you have a script-only stack called button1 behavior that contains the behavior for button1.
script "button1 behavior"
You assign the flash behavior by adding with behavior "flash"
to the script
line:
script "button1 behavior" with behavior "flash"
on mouseUp
flash
answer "in mouseUp"
end mouseUp
In the behaviors
folder, there is a script-only stack behavior1.livecodescript
. Its contents are
script "behavior1"
on loadBehavior
set the behavior of this stack to the long id of stack "behavior2"
end loadBehavior
... and another stack called behavior2.livecodescript
. Its contents are
script "behavior2"
on loadBehavior
set the behavior of this stack to the long id of stack "behavior3"
end loadBehavior
on hello
answer "hello"
end hello
You have now chained the behaviors together. If behavior1
receives a message that it does not handle (such as hello
), it will be passed to behavior2
. If behavior2
does not handle the message, it will be passed to behavior3
.
Levure is an application development framework written for LiveCode.