Logic: Scripting and the controller
Learning objectives
Calculations are often required to run logic on the client. They are performed using script which provides the required assignment, conditional branching, and looping. We have seen some of this in previous lessons when we discussed UI events, UI actions, and data actions.
In this lesson, we are introduced to the concepts of:
- Controller
- Model
- View
- Client-side expressions
In addition to the raw scripting capabilities, the model, the view, and the controller are the objects on the client that the script can interact with. The controller provides many of the important client-side scripting capabilities.
Controller
The controller's main capabilities are discussed in UX Scripting. In script within a dialog, portlet, or layout, the controller can be referred to as "this" or by using the @
symbol.
Examples
The following code:
(@ confirm : "This is my message" "This is my caption" '("Ok" "Continue"))
displays a message box and returns the button that was selected:
or
(@ flash : "Hello World")
displays:
The controller can also invoke domain model events. The following example invokes the markAsComplete
method on the currently selected item.
(@ invoke : (@ model item) 'markAsComplete)
Many of the controller's capabilities allow you to get hold of a model or a view.
Model
Models contain the client-side data and are typically bound to business model collections and instances.
Examples
The current instance can be accessed with:
(@ model)
Once you have a model, you can access the current item with 'item
and any attributes of that item with the attribute's name.
(define current ((@ model)'item))
(if (= (current'lastName) "Lamont")
...
You can also use (@ findModel
to find the model of a given view or controller. Every view has an associated model, so you can use this function to inspect what is going on with lists, combos, radio buttons, and so on.
Instance and collection models
We will be adding a discussion here about instance models and collection models in the near future. These are merely the different properties and methods of the model, depending on if you are dealing with a single item or a collection of items.
View
Views are made up of controls, their parents, and their children.
Examples
To get hold of a view, use:
(@ findView : 'frmDetail)
This code finds a view by name. It is important to use meaningful and unique names for elements in your portlets, dialogs, and layouts. Once you find your view, you can work with its properties for visibility, captions, styles, and so on. We saw this in Behavior: UI events, UI actions, and data actions where we wrote a script similar to the following:
(define myView (@ findView : 'table))
(logger'info "Selection changed to " (@ model item lastName))
(myView'caption (string-append "Selection changed to " (@ model item lastName)))
Client-side expressions
In addition to the model, view, and controller, you can use scripting in client-side expressions. These can be useful for captions, visibility binds, and calculated attributes such as fullName
. Client-side expressions are usually in the form of binds or expressions.
Examples
At its simplest, an expression is a constant "Buongiorno Mondo!"
If it is an expression, it must start with a back-tick or quasiquote character (on an English keyboard, usually in the top-right corner; you can also use ALT-096).
`(string-append "Buongiorno " "Mundo!")
You can also use client-side expressions to insert content from the business model. To do this, simply use a comma to escape your business model fields.
`(string-append "Buongiorno " ,(@ fullName))
Are the query attributes still manually required for a layout? I found that in the example above, the system was smart enough to know that I wanted firstName in my query to support the calculation.