This blog post was created with AI assistance to help developers understand Uniface concepts better.
π What is Component Scripting?
In Uniface 10.4, scripting component behavior means adding custom actions and logic to your application components. While Uniface provides lots of default behavior (especially for data handling, locking, and transactions), you can enhance or override this behavior using ProcScript.
π οΈ Getting Started: The Write Script Worksheet
The Write Script worksheet is your main workspace for adding functionality to components. Here's where you define how your component responds to events, validates data, and executes actions.
How to Access It
- Click the Write Script tab in the Component Editor
- In the Structure pane, select the component, entity, or field you want to script
- The Code Editor will display two code containers: Declarations and Script
π‘ Understanding Script Modules
Uniface uses two main types of script modules to organize your code:
π― Triggers
Triggers are code blocks that execute automatically when specific events occur. Think of them as event handlers that respond to user actions or system events.
Example:
trigger detail
message "You clicked on a detail!"
end
βοΈ Operations
Operations are reusable pieces of code that you can call from anywhere in your component. They're like functions or methods in other programming languages.
Example:
operation CalculateTotal
variables
numeric vTotal
endvariables
vTotal = AMOUNT * QUANTITY
return vTotal
end
π Using Code Snippets
Uniface makes scripting easier with built-in code snippets. Here's how to use them:
- Open the Resource Browser
- Select a Snippet Library (HTML or ProcScript)
- Right-click the snippet you need
- Select Insert
This inserts a ready-to-use template that you can customize for your needs!
π Important Concept: Code Inheritance
If your entities and fields are modeled and already contain code, that code is inherited by the component. However, there's an important rule: if you write script in a trigger that has inherited code, your new code overrides the modeled code.
Real-World Example
Let's say you want to validate user input before saving data:
trigger detail
variables
string vName
endvariables
vName = CUSTOMER_NAME
if (vName = "")
message "Customer name cannot be empty!"
return -1
endif
message "Data is valid β"
end
π Dynamic Server Pages: JavaScript Support
For web applications using Dynamic Server Pages (DSP), you can also use JavaScript for client-side behavior. Uniface provides a JavaScript API that lets you interact with Uniface objects directly from JavaScript code.
β¨ Key Takeaways
- Use the Write Script worksheet to add custom behavior to components
- Triggers respond to events automatically
- Operations are reusable code blocks you can call manually
- Code snippets speed up development
- Inherited code gets overridden when you write new code in the same trigger
- DSP components support both ProcScript and JavaScript
π Best Practices
When scripting component behavior, remember these tips:
- Start with Uniface's default behavior and only override what you need
- Use meaningful names for triggers and operations
- Keep your code organized by using the Structure pane effectively
- Test your scripts thoroughly before deployment
- Use code snippets as starting templates to maintain consistency
π¦ Next Steps
Now that you understand the basics of scripting component behavior, try these exercises:
- Create a simple form component
- Add a trigger to validate field input
- Create an operation to perform a calculation
- Use a code snippet from the Resource Browser
- Test your component using the Compile and Test function
Happy coding! π¨βπ»π©βπ»
Top comments (0)