[Tip] Creating records
There are a number of ways to create new records and assign data in Servoy. The most direct and obvious approach is:
controller.newRecord()
field1 = "something"
Closely related is:
foundset.newRecord()
foundset.field1 = "something"
The difference between using controller and foundset is that foundset doesn't update the UI until the operation is completed. This makes the foundset approach idea when creating many records at once (much faster).
We prefer a third approach: creating the record and storing it in a variable in one step:
var record = foundset.getRecord(foundset.newRecord())
record.field1 = "something"
The main advantage with this approach is that creating related records can be quite a bit more readable. The side benefit is that you can save each record individually with:
databaseManager.saveData(record)
when done assigning data. (If you don't pass in a record object to the saveData() function, Servoy does a general save which can be more time consuming.)
Here is an example snippet from our Sutra CMS that creates records up to two relations deep. Note that using our preferred approach to creating records, we avoid having to string together multiple relations like:
foundset.web_theme_to_layout.web_layout_to_editable.newRecord()
foundset.web_theme_to_layout.web_layout_to_editable.editable_name = _themes[_themesSelected].editables[i][j]
Much more readable this way:
// 1 create theme record
var theme = foundset.getRecord(foundset.newRecord())
theme.theme_name = _themes[_themesSelected].name
theme.description = _themes[_themesSelected].description
var themeDirectory = _themes[_themesSelected].path.split("/")
theme.theme_directory = themeDirectory[themeDirectory.length - 1]
theme.activated = 1
databaseManager.saveData(theme)
for (var i in _themes[_themesSelected].editables ) {
// 2 create layout record
var layout = theme.web_theme_to_layout.getRecord(theme.web_theme_to_layout.newRecord())
layout.layout_path = i
layout.layout_name = i.split(".")[0]
if (i == "default.jsp") layout.flag_default = 1
databaseManager.saveData(layout)
for (var j in _themes[_themesSelected].editables[i] ) {
// 3 create editable area record
var editable = layout.web_layout_to_editable.getRecord(layout.web_layout_to_editable.newRecord())
editable.editable_name = _themes[_themesSelected].editables[i][j]
databaseManager.saveData(editable)
}
}
Comments (2)