Since K2 smartforms 1.0.6, there is a new event, When the Form is Initialized, which runs after When the Form is Initializing completes. The new event allows the Designer to configure rules that runs after a form has been loaded (as ajax calls). This is an important feature as we can now load the form first (forms becomes interactive), then make X number of ajax calls to load the other controls/Views data (e.g. dropdown, List View, etc), instead of loading everything when the form loads. So what does this mean? Let me show you a simple example.
Firstly, let’s create a SmartObject that simulate a large result set or simply a very slow data source (imagining my Oracle and SAP in dev env…). I’ll be using a stored procedure that simulates a 5 seconds wait before returning my result set.
CREATE PROCEDURE [dbo].[sp_vSlowScript]
WAITFOR DELAY '00:00:05'
DECLARE @tmpTbl TABLE (
INSERT INTO @tmpTbl VALUES ('A1', 'B1', 'C1');
INSERT INTO @tmpTbl VALUES ('A2', 'B2', 'C2');
INSERT INTO @tmpTbl VALUES ('A3', 'B3', 'C3');
SELECT * FROM @tmpTbl
As the procedure name says, it is a very slow script and the corresponding spVSlowScript SmartObject is generated.
Next, I generate a View from spVSlowScript SmartObject and made sure to remove all Rules in the View. I don’t need the When the View is Initializing rule.
With the View ready, I created a new Form and added 2 instances of the View into it.
Now, move on to the Form Rules configuration. You should see the When the Form is Initializing rule added for you. Let’s edit this rule and change the views method to List. Change the execution block type to also for efficiency (You should use and block if one action returns much faster than the other).
With the Rule above, the Form will finish loading when both the actions have been completed. Save the Form now.
Testing the Form Initializing rule
In this test, all loading actions (especially the slow view methods) are executed in the initialising rule.
Run the Form URL now and you will notice that the spinner overlay will cover the entire form and the form will only be usable/interactive after the 2 List methods completed the ajax call, which is after 5 seconds. The main interest point here is that your Form takes more than 5 seconds to load, which will fail most of your performance requirements. =(
Testing the Form Initialized rule
Now, let’s try changing the When the Form is Initialising rule to When the Form is Initialized and see the difference.
When you run the form again, you will realise that the form actually loads almost immediately (<1 second) and each View will have a spinner overlay to indicate that it is performing an operation now (ajax call). While the spinner overlays are running, the User can start to interact with any controls that have completed loading (e.g. text boxes, etc). Now, you have passed your performance test. =)
Form is Initializing or Initialized?
So in short, you should place the actions that are required on form load in When the Form is Initializing rule and any other actions that could slow down a form loading or could be loaded at a later stage to the When the Form is Initialized rule.
The following is a list of actions that you can consider for the 2 events.
Some example of actions that could be added to the When the Form is Initializing rule
- OpenWorklistItem action.
- SmartObject or View methods that loads the main form content. (e.g. The Leave object in your Leave Application)
- SmartObject or View methods that returns small number of records and the performance is good.
- Hide/show controls and views. If you already know what to hide or show at this stage, you should do it here.
Some example of actions that could be added to the When the Form is Initialized rule
- SmartObject or View methods that does not load the main form content and is slow in performance (e.g. large result set that takes a considerable time to load).
- Hide/show controls and views. If the consideration logics are finalised after all actions in the initialising and initialised rule are completed. These will be added to the end of the initialised rule.