Child pages
  • Solution Model

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Table of contents

Table of Contents


Purpose

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:

  • Create objects and set their properties
  • Clone objects
  • Manipulate properties of existing objects
  • Revert objects to its original design-time state
  • Remove existing objects


Basic rules and functionality

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
titleNote

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.


Examples

This section shows (simple) examples of how different objects can be controlled with the Solution Model. Per object you will find how to:

  • Create an object
  • Retrieve an existing object
  • Change a property of an object
  • Remove an existing object


Calculations

To create a new calculation with name myCalculation on table customers of server example_data which returns 1:

Code Block

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:

Code Block

var calculation = solutionModel.getCalculation('myCalculation', 'db:/example_data/customers');

To output whether or not this calculation is a stored one:

Code Block

application.output('Stored calculation: ' + calculation.isStored();

To remove existing calculation myCalculation:

Code Block

solutionModel.removeCalculation('myCalculation', 'db:/example_data/customers');


Styles

To create a new stylesheet with name myStyle with a default style class for forms:

Code Block

var style = solutionModel.newStyle('myStyle' 'form { background-color: transparent; }';

To get existing stylesheet myStyle:

Code Block

var style = solutionModel.getStyle('myStyle')

To add a default style class for fields:

Code Block

style.text += 'field { background-color: blue; }';

To remove existing stylesheet myStyle:

Code Block

solutionModel.removeStyle('myStyle');


Global variables

To create a new global variable with name myGlobalVariable of type TEXT:

Code Block

var globalVariable = solutionModel.newGlobalVariable('myGlobalVariable', JSVariable.TEXT);

To get existing global variable myGlobalVariable:

Code Block

var globalVariable = solutionModel.getGlobalVariable('myGlobalVariable');

To change its default value to abc:

Code Block

myGlobalVariable.defaultValue = 'abc';

To remove existing global variable myGlobalVariable:

Code Block

solutionModel.removeGlobalVariable('myGlobalVariable');


Global methods

To create a new global method with name myGlobalMethod:

Code Block

var globalMethod = solutionModel.newGlobalMethod('function myGlobalMethod() { currentcontroller.newRecord(); }');

To get existing global method myGlobalMethod:

Code Block

var globalMethod = solutionModel.getGlobalMethod('myGlobalMethod');

To make it appear in the menu:

Code Block

globalMethod.showInMenu = true;

To remove existing global method myGlobalMethod:

Code Block

solutionModel.removeGlobalMethod('myGlobalMethod');


Forms

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:

  • Create new forms
  • Get existing forms
  • Clone an existing form
  • Remove an existing form
  • Revert an existing form to its original design-time state

To create a new form with name myForm:

Code Block

var form = solutionModel.newForm('myForm');

To get existing form myForm:

Code Block

var form = solutionModel.getForm('myForm');

To disable its navigator:

Code Block

form.navigator = SM_DEFAULTS.NONE;
Note
titleNote

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:

  • when controller.recreateUI() is called on a form, all elements are recreated based on the solutionModel for that form. This means that any runtime changes to the elements are lost, like a dynamically added tab with elements.myTabPanel.addTab().
  • Any reference to the elements on the form that is stored will become invalid, as the element is recreated.
  • Function controller.recreateUI() cannot be used while a Drag 'n' Drop operation is underway on the form.

To clone existing form myForm to cloned form myClonedForm:

Code Block

var form = solutionModel.getForm('myForm');
var clonedForm = solutionModel.cloneForm('myClonedForm', form);

To remove existing form myForm:

Code Block

var success = history.removeForm('myForm');
if (success) {
	solutionModel.removeForm('myForm');
}

To revert existing form myForm to its original design-time state:

Code Block

var success = history.removeForm('myForm');
if (success) {
	solutionModel.removeForm('myForm');
}
Note
titleNote

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
titleNote

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.


Form variables

To create a new form variable with name myFormVariable:

Code Block

var formVariable = form.newFormVariable('myFormVariable', JSField.TEXT_FIELD);

To get existing form variable myFormVariable:

Code Block

var formVariable = form.getFormVariable('myFormVariable');

To change its default value to abc:

Code Block

formVariable.defaultValue = 'abc';

To remove existing form variable myFormVariable:

Code Block

form.removeFormVariable('myFormVariable');


Form methods

To create a new form method with name myFormMethod:

Code Block

var formMethod = form.newFormMethod('function myFormMethod() {controller.newRecord(); }');

To get existing form method myFormMethod:

Code Block

var formMethod = form.getFormMethod('myformMethod');

To make it appear in the menu:

Code Block

globalMethod.showInMenu = true;

To remove existing form method myFormMethod:

Code Block

form.removeFormMethod('myFormMethod');


Beans

To create a tree view with name myBean:

Code Block

var bean = form.newBean('myBean', 'com.servoy.extensions.beans.dbtreeview.DBTreeView', 200, 200, 300, 300);

To get existing bean: myBean:

Code Block

form.getBean('myBean');

To change its anchoring to top, left and bottom:

Code Block

bean.anchors = SM_ANCHOR.NORTH | SM_ANCHOR.WEST | SM_ANCHOR.SOUTH;

To remove existing bean myBean:

Code Block

form.removeBean('myBean');


Buttons

To create a new button with name myButton and text Text and attaching global method myGlobalMethod to it:

Code Block

var globalMethod = solutionModel.getGlobalMethod('myGlobalMethod');
var button = form.newButton('Text', 0, 0, 80, 20, globalMethod);
button.name = 'myButton';

To get existing button myButton:

Code Block

var button = solutionModel.getButton('myButton');

To change its height to 30"

Code Block

button.height = 30;

To remove existing button myButton:

Code Block

form.removeButton('myButton');


Components

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:

Code Block

var component = form.getComponent('myButton');

To hide the element:

Code Block

component.visible = true;

To retrieve all available components on the form:

Code Block

var components = form.getComponents();

To change the width of all elements to 200:

Code Block

for (var i = 0; i < components.length; i++) {
	components[i].width = 200;
}


Fields

To create a new text field with name myField with form variable myFormVariable as its dataprovider:

Code Block

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:

Code Block

var field = form.getField('myField');

To change its display type to a text area:

Code Block

field.displayType = JSField.TEXT_AREA; 

To remove existing field myField:

Code Block

form.removeField('myField');


Labels

To create a new label with name myLabel with text Text:

Code Block

var label = form.newLabel('Text', 0, 0, 100, 20);
label.name = 'myLabel';

To retrieve existing label myLabel

Code Block

var label = form.getLabel('myLabel');

To change its horizontal alignment to center:

Code Block

var label = form.getLabel('myLabel');

To change its horizontal alignment to center:
label.horizontalAlignment = SM_ALIGNMENT.CENTER;

Code Block

To remove existing label _myLabel_:

form.removeLabel('myLabel');

Code Block

\\
h5. Portals

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);

Code Block

To get existing portal _myPortal_:

var portal = form.getPortal('myPortal');

Code Block

To make it resizable:

portal.resizable = true;

Code Block

To remove existing portal _myPortal_:

form.removePortal('myPortal');

Code Block

\\
h5. Tab panels

To create a new tab with name _myTabPanel_:

var tabPanel = form.newTabPanel('myTabPanel', 0, 0, 500, 500);

Code Block

To get existing tab panel _myTabPanel_:

var tabPanel = form.getTabPanel('myTabPanel);

Code Block

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);

Code Block

To remove existing tab panel _myTabPanel_:

form.removeTabPanel('myTabPanel');

Code Block

\\
h4. Relations

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);

Code Block

To get existing relation _myRelation_:

var relation = solutionModel.getRelation('myRelation');

Code Block

To create new a new relation item based on fields _id_ and _customer_id_:

relation.newRelationItem('id', '=', 'customer_id');

Code Block

To remove existing relation _myRelation_:

solutionModel.removeRelation('myRelation');

Code Block

\\
h4. Value lists

To create a new value list with name _myValueList_:

var valueList = solutionModel.newValueList('myValueList', JSValueList.CUSTOM_VALUES);

Code Block

To get existing value list _myValueList_:

var valueList = solutionModel.getValueList('myValueList');

Code Block

To set custom values for the value list:

valueList.customValues = '1\n2';

Code Block

To remove existing value list _myValueList_:

solutionModel.removeValueList('myValueList');

Code Block

\\
h4. Media

To create new media with name _myMedia_:

var media = solutionModel.newMedia('myMedia', plugins.http.getMediaData('http://www.servoy.com/images/logo_servoy.gif'));

Code Block

To get existing media _myMedia_:

var media = solutionModel.getMedia('myMedia');

Code Block

To change it content:

media.bytes = plugins.http.getMediaData('http://servoy.com/images/headerimages/open_source.jpg');

Code Block

To remove existing media _myMedia_:

solutionModel.removeMedia('myMedia');

Code Block