The Solution Model is a feature since Servoy version 4.1 which allows you to manipulate different kinds of objects on-the-fly through scripting. You can perform actions such as:
With the Solution Model you can control different types of objects. Referring to these objects is done through so-called JS Objects and consist of constants, properties and/or functions.
The following list shows the objects which can be controlled by the Solution Model, its corresponding JS Object and from which JS Object it needs to be referenced from in order to use it:
Object |
JS Object |
Referenced from JS Object |
---|---|---|
Calculations |
JSCalculation |
|
Styles |
JSStyle |
|
Global variables |
JSVariable |
|
Global methods |
JSMethod |
|
Forms |
JSForm |
|
Form variables |
JSVariable |
JSForm |
Form methods |
JSMethod |
JSForm |
Beans |
JSBean |
JSForm |
Buttons |
JS Button |
JSForm |
Components |
JSComponent |
JSForm |
Fields |
JSField |
JSForm |
Labels |
JSLabel |
JSForm |
Parts |
JS Part |
JSForm |
Portals |
JSPortal |
JSForm |
Tab panels |
JSTabPanel |
JSForm |
Relations |
JSRelation |
|
Value lists |
JSValueList |
|
Media |
JSMedia |
|
Note
A list of all these JS Objects and their corresponding constants, properties and functions can be found under the SolutionModel node of the Solution Explorer.
When creating objects you need to make sure that the same object with the same name does not already exists. In this case you need to choose another name or remove the existing object.
By default, changes made by the Solution Model are not persistent and are only valid for the current session in which these changes are made. However, by a special setting (servoyDeveloper scripting access?) the changes are being stored back into the workspace.
This section shows (simple) examples of how different objects can be controlled with the Solution Model. Per object you will find how to:
To create a new calculation with name myCalculation on table customers of server example_data which returns 1:
var calculation = solutionModel.newCalculation('function myCalculation() { return 1; }', JSVariable.INTEGER, 'db:/example_data/customers');
To get the existing calculation myCalculation on table customers of server example_data:
var calculation = solutionModel.getCalculation('myCalculation', 'db:/example_data/customers');
To output whether or not this calculation is a stored one:
application.output('Stored calculation: ' + calculation.isStored();
To remove existing calculation myCalculation:
solutionModel.removeCalculation('myCalculation', 'db:/example_data/customers');
To create a new stylesheet with name myStyle with a default style class for forms:
var style = solutionModel.newStyle('myStyle' 'form { background-color: transparent; }';
To get existing stylesheet myStyle:
var style = solutionModel.getStyle('myStyle')
To add a default style class for fields:
style.text += 'field { background-color: blue; }';
To remove existing stylesheet myStyle:
solutionModel.removeStyle('myStyle');
To create a new global variable with name myGlobalVariable of type TEXT:
var globalVariable = solutionModel.newGlobalVariable('myGlobalVariable', JSVariable.TEXT);
To get existing global variable myGlobalVariable:
var globalVariable = solutionModel.getGlobalVariable('myGlobalVariable');
To change its default value to abc:
myGlobalVariable.defaultValue = 'abc';
To remove existing global variable myGlobalVariable:
solutionModel.removeGlobalVariable('myGlobalVariable');
To create a new global method with name myGlobalMethod:
var globalMethod = solutionModel.newGlobalMethod('function myGlobalMethod() { currentcontroller.newRecord(); }');
To get existing global method myGlobalMethod:
var globalMethod = solutionModel.getGlobalMethod('myGlobalMethod');
To make it appear in the menu:
globalMethod.showInMenu = true;
To remove existing global method myGlobalMethod:
solutionModel.removeGlobalMethod('myGlobalMethod');
Forms are most commonly used with the Solution Model and have some additional functions. The following basic functions are available for this type of objects:
To create a new form with name myForm:
var form = solutionModel.newForm('myForm');
To get existing form myForm:
var form = solutionModel.getForm('myForm');
To disable its navigator:
form.navigator = SM_DEFAULTS.NONE;
Note
When changing an existing form which has already been loaded, you need to refresh it before the end of the method by using controller.recreateUI(). If the form is not refreshed then this will result in an error.
You also need to take the following into consideration:
To clone existing form myForm to cloned form myClonedForm:
var form = solutionModel.getForm('myForm'); var clonedForm = solutionModel.cloneForm('myClonedForm', form);
To remove existing form myForm:
var success = history.removeForm('myForm'); if (success) { solutionModel.removeForm('myForm'); }
To revert existing form myForm to its original design-time state:
var success = history.removeForm('myForm'); if (success) { solutionModel.removeForm('myForm'); }
Note
Before removing or reverting the form by using the Solution Model it's important to remove any active instances of this form from the history stack.
Note
You can only revert a form when it exists as a physical form created in design-time. Reverting a form which is created by the Solution Model will result in an error.
To create a new form variable with name myFormVariable:
var formVariable = form.newFormVariable('myFormVariable', JSField.TEXT_FIELD);
To get existing form variable myFormVariable:
var formVariable = form.getFormVariable('myFormVariable');
To change its default value to abc:
formVariable.defaultValue = 'abc';
To remove existing form variable myFormVariable:
form.removeFormVariable('myFormVariable');
To create a new form method with name myFormMethod:
var formMethod = form.newFormMethod('function myFormMethod() {controller.newRecord(); }');
To get existing form method myFormMethod:
var formMethod = form.getFormMethod('myformMethod');
To make it appear in the menu:
globalMethod.showInMenu = true;
To remove existing form method myFormMethod:
form.removeFormMethod('myFormMethod');
To create a tree view with name myBean:
var bean = form.newBean('myBean', 'com.servoy.extensions.beans.dbtreeview.DBTreeView', 200, 200, 300, 300);
To get existing bean myBean:
form.getBean('myBean');
To change its anchoring to top, left and bottom:
bean.anchors = SM_ANCHOR.NORTH | SM_ANCHOR.WEST | SM_ANCHOR.SOUTH;
To remove existing bean myBean:
form.removeBean('myBean');
To create a new button with name myButton and text Text and attaching global method myGlobalMethod to it:
var globalMethod = solutionModel.getGlobalMethod('myGlobalMethod'); var button = form.newButton('Text', 0, 0, 80, 20, globalMethod); button.name = 'myButton';
To get existing button myButton:
var button = solutionModel.getButton('myButton');
To change its height to 30:
button.height = 30;
To remove existing button myButton:
form.removeButton('myButton');
If you want to change properties which are common to all elements, you can do this through components instead of having to retrieve each individual object separately.
To retrieve component with name myButton:
var component = form.getComponent('myButton');
To hide the element:
component.visible = true;
To retrieve all available components on the form:
var components = form.getComponents();
To change the width of all elements to 200:
for (var i = 0; i < components.length; i++) { components[i].width = 200; }
To create a new text field with name myField with form variable myFormVariable as its dataprovider:
var formVariable = form.getFormVariable('myFormVariable'); var field = form.newField(formVariable, JSField.TEXT_FIELD, 0, 0, 100, 200); field.name = 'myField';
To get existing field myField:
var field = form.getField('myField');
To change its display type to a text area:
field.displayType = JSField.TEXT_AREA;
To remove existing field myField:
form.removeField('myField');
To create a new label with name myLabel with text Text:
var label = form.newLabel('Text', 0, 0, 100, 20); label.name = 'myLabel';
To get existing label myLabel:
var label = form.getLabel('myLabel');
To change its horizontal alignment to center:
label.horizontalAlignment = SM_ALIGNMENT.CENTER;
To remove existing label myLabel:
form.removeLabel('myLabel');
To create a new portal with name myPortal based on relation myRelation:
var relation = solutionModel.getRelation('myRelation'); var portal = form.newPortal('myPortal', relation, 0, 0, 500, 500);
To get existing portal myPortal:
var portal = form.getPortal('myPortal');
To make it resizable:
portal.resizable = true;
To remove existing portal myPortal:
form.removePortal('myPortal');
To create a new tab with name myTabPanel:
var tabPanel = form.newTabPanel('myTabPanel', 0, 0, 500, 500);
To get existing tab panel myTabPanel:
var tabPanel = form.getTabPanel('myTabPanel);
To add a new tab with name myTab based on relation myRelation:
var relation = solutionModel.getRelation('myRelation'); var tab = tabPanel.newTab('myTab', 'Text', myRelatedForm, relation);
To remove existing tab panel myTabPanel:
form.removeTabPanel('myTabPanel');
To create a new relation with name myRelation between tables customers and orders:
var relation = solutionModel.newRelation('myRelation', 'db:/example_data/customers', 'db:/example_data/orders', JSRelation.INNER_JOIN);
To get existing relation myRelation:
var relation = solutionModel.getRelation('myRelation');
To create new a new relation item based on fields id and customer_id:
relation.newRelationItem('id', '=', 'customer_id');
To remove existing relation myRelation:
solutionModel.removeRelation('myRelation');
To create a new value list with name myValueList:
var valueList = solutionModel.newValueList('myValueList', JSValueList.CUSTOM_VALUES);
To get existing value list myValueList:
var valueList = solutionModel.getValueList('myValueList');
To set custom values for the value list:
valueList.customValues = '1\n2';
To remove existing value list myValueList:
solutionModel.removeValueList('myValueList');
To create new media with name myMedia:
var media = solutionModel.newMedia('myMedia', plugins.http.getMediaData('http://www.servoy.com/images/logo_servoy.gif'));
To get existing media myMedia:
var media = solutionModel.getMedia('myMedia');
To change it content:
media.bytes = plugins.http.getMediaData('http://servoy.com/images/headerimages/open_source.jpg');
To remove existing media myMedia:
solutionModel.removeMedia('myMedia');