Configure the Initialized Event
You can use the Initialized event to improve the user experience when a view or form is loaded, for example to populate a drop-down list with data in the background while the rest of the view is available for data entry.
The Initialized event executes after the view or form's loading spinner stops. Use the Initialized event with rule actions that don't have to wait for the view or form spinner to finish. For example, List controls can load their list values without waiting for a loading/spinner that blocks the view or form. They contain their own spinners to indicate when they are not fully loaded, and can be used with the Initialized event to load after the view or form has loaded.
You can also use the Initialized event with the Display Field property of list controls to speed up the first time a view or form is used when you're loading data. The Display Field property allows you to show the control's display value before its data loads. See Configuring the Display Field property for an example of configuring the Display Field property to load an initial value in a drop-down list.
This section illustrates how to configure an initialized rule. Use one of the following events in a rule and select Initialized as the event:
- When the Form raises an event
- When a View raises an event
In this example, you have a view containing two drop-down controls called Customer Drop-Down List and Product Drop-Down List. When you configure drop-down controls on a view, K2 automatically creates a default rule called When the View executed Initialize, which populates the drop-down controls when the view opens. The image below shows the default Initialize rule for this example.
Default rule configuration
If one of the controls such as the Product Drop-Down List contains many records, the populate action may cause a performance impact when the view loads because the form is locked until both drop-down lists are populated with data. To improve the user experience and speed up the load time of the view, you can move the then populate Product Drop-Down List list control with data rule action to an Initialized rule which executes after the view has loaded. This allows you to populate the Customer Drop-Down List control when the view opens and make the view available for editing once the view spinner stops. When the view spinner stops and the view has loaded, the Product Drop-Down List control is populated. The images below show what the enhanced configuration looks like.
Enhanced configuration containing the Initialize and Initialized rules
Enhanced rule configuration of the Initialize event containing the populate Customer Drop-Down List control action
Enhanced rule configuration of the Initialized event containing the populate Product Drop-Down List control action
The sequence of the rule definition execution is explained below:
- The view loads (Initialize).
- The Customer Drop-Down List populates with data. The view spinner shows while populating the drop-down list. When the spinner stops, the view is available for editing and you can access the list of customers.
- After the view has loaded (Initialized) and the view spinner has stopped, the Product Drop-Down List populates with data. The control spinner shows while populating the drop-down list. When the spinner stops, you can access the list of products.
Follow the steps below to configure the rules shown above.
- Edit the view, go to the Rules tab and edit the default When the View executed Initialize rule.
- Select the then populate Product Drop-Down List list control with data action and click Remove. When the view opens, only the Customer Drop-Down List control is populated. Click OK to save the rule.
- Click Add Rule to add another rule.
- In the Events tab, click the When the View executes a method event in the View Events section, and then select Initialized as the event, as shown below.
- Select the Actions tab, click the Populate a list control with data action in the Data Transfer section, and then select the Product Drop-Down List control as shown below. Click OK to save the rule. When the view has loaded and the Customer Drop-Down List control is populated, the view is available for editing and the Product Drop-Down List control is populated.
- The Initialized event always executes after the related Initialize event, regardless if there are user actions configured for the Initialize event or if the Initialize event is disabled. Initialized always executes after Initialize even if Initialized is called from any other rule. See the note at the top of this page for information about the relation between the Initialize and Initialized events.
- When you add or remove the When the View executed Initialized event (Initialized) on a form and the When the View executed Initialize event (Initialize) exists on the view contained within the form, the Initialized event executes when the Initialize event has completed.
- When you have a When the View executed Initialized event (Initialized), When the View executed Initialize event (Initialize), When the Form is Initialized event (Initialized), and When the Form is Initializing event (Initialize), the view and form execution happens independently. The form execution does not wait for the view execution to complete.
- Use the initialize and initialized rules to improve performance on the initial load of a form (when the form opens for the first time). When you use these rules after a form has initially loaded, for example when you click a button, you may experience unexpected or different behavior. Consider the following when using these rules
The rules are dependent on the state in which the view or form is when the view or form loads. If the form state changes and the rules execute, the behavior may be different to when you first load the view or form.
It is recommended to use these rules when you first load a view or form to improve performance. For greater control and visibility you can separate actions and conditions into separate rules.
When you use the initialize and initialized rules together they create a chain. If you want to execute advanced actions and conditions or want to specify that rules execute in an exact order you must rather use the Execute another rule action and Unbound rule concepts.