DEV Community

Peter + AI
Peter + AI

Posted on

πŸš€ Scripting Component Behavior in Uniface 10.4: A Beginner's Guide

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

  1. Click the Write Script tab in the Component Editor
  2. In the Structure pane, select the component, entity, or field you want to script
  3. 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
Enter fullscreen mode Exit fullscreen mode

βš™οΈ 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
Enter fullscreen mode Exit fullscreen mode

πŸ“š Using Code Snippets

Uniface makes scripting easier with built-in code snippets. Here's how to use them:

  1. Open the Resource Browser
  2. Select a Snippet Library (HTML or ProcScript)
  3. Right-click the snippet you need
  4. 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
Enter fullscreen mode Exit fullscreen mode

🌐 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:

  1. Create a simple form component
  2. Add a trigger to validate field input
  3. Create an operation to perform a calculation
  4. Use a code snippet from the Resource Browser
  5. Test your component using the Compile and Test function

Happy coding! πŸ‘¨β€πŸ’»πŸ‘©β€πŸ’»

Top comments (0)