Basic Controller Programming

In a typical Web 1.0 view with JavaScript customization, the Controller is implemented as a set of JavaScript functions:

//...variables go here...
var requiresParking = true;

function user_form_onload() {
//...application initialization code goes here...
}
function onNext() {
//...event handling code goes here...
}


These functions are attached to the user interface controls in an AXVW file:

<action id="next">
   <title>Next</title>
   <command type="callFunction" functionName="onNext"/>
</action>


This coding style is easy to learn and convenient if you need to add two or three simple JavaScript event handlers to the view, and it is fully supported in View 2.0 Format. However, for any sizable application this style tends to result in a large amount of unstructured JavaScript code that makes the application difficult to maintain or extend. The following issues are typical:

To provide a better code organization we recommend using a controller object. The general template for the controller is shown below:

var addEmployeeController = View.createController('addEmployee', {
    //...variables go here...
    requiresParking: true,

    afterViewLoad: function() {
        this.inherit();
        ...application initialization code goes here...
    },

    employeeForm_next_onClick: function() {
        ...event handling code goes here...
    }
});


Note that the controller code uses JavaScript object syntax:

Once created, the controller works in concert with the Web Central view. The view will automatically call the afterViewLoad function when its loading process is complete, giving the controller a chance to do something useful � for example:

afterViewLoad: function() {
    this.inherit();

    // application-specific code called on view load
    View.panels.get('employeeForm').newRecord = true;
}