Reducing Object references with WITH

As the Servoy’s code editor supports Javascript and in-line Java, you can use almost all the Javascript keywords with in Servoy. Among the powerful Javascript keywords, “WITH” keyword is used to reduce Object references in your code dramatically. You can also make your code more readable by using WITH.

This TIP will explain you the use of WITH in Servoy.

Using WITH Statement, you can run multiple statements with the same Object reference, without specifying the Object reference at every time in your code. Let’s see How we can reduces the Object reference with WITH.

var lbl = solutionModel.getForm("customers").newLabel("New Label",100,200,100,20)

lbl.name = “lbl_newLable”;

lbl.background = “#FFAA65”;

lbl.borderType = “Line”;

lbl.fontType = “Calibri,1,14”

lbl.transparent = false;

lbl.foreground = ‘#0000ff’;

lbl.toolTipText = “New Leble : generated by using SolutionModel.”;

In the above code, I am adding a label to the form, customers, and setting some property for the same, by using solution model. Here, I am using the same label reference each time for setting the properties for the label. By using WITH statement, we can set all the properties of the label with out using the reference each time. Let’s see how can we do that.

with (solutionModel.getForm("customers").newLabel("New Label",100,200,100,20))

{

name = “lbl_newLable”;

background = “#FFAA65”;

borderType = “Line”;

fontType = “Calibri,1,14”

transparent = false;

foreground = ‘#0000ff’;

toolTipText = “New Leble : generated by using SolutionModel.”;

}

Now, you can set every property of the label with out using the Object reference at all. WITH will take of everything.

You can use any conditional statements or any other codes with in WITH statement. You can even use nested WITH statements in your code.

150 150 Burnignorance | Where Minds Meet And Sparks Fly!