Manipulating Objects from JavaScript
Creating objects
A new object can be created using the syntax DB.object_type.create()
.
As an example, suppose we have an object type "person" with an attribute "name".
We would define a variable for the object, and initialise it as follows:
View XML:
... and View JavaScript:
All objects that are defined as variables in a view are saved automatically whenever a view is dismissed, but not when the user presses the back button. However, when defining an object only in JavaScript (and not in the view), you need to save the object manually:
Setting dates and times
Both date
and datetime
attributes are represented as the JavaScript Date
type. To set a date or datetime to the
current date/time, use new Date()
. To set it to a specific date, use new Date(year, month, day)
. For more details,
see this article at Mozilla.
Examples:
Deleting objects
An object can be deleted using its destroy()
function.
As an example, suppose we have an object type "person":
View XML:
... and View JavaScript:
Setting relationships
Unlike attributes, a relationship is set using a setter function.
As an example, suppose that in our Data Model every person is part of a household:
In our view we define our household and person:
... and initialise them in JavaScript:
As you can see, we set the "household" relationship on the "member" by calling the household()
function and passing the related "household" object as an argument.
As a more typical example, the household will be created in another view and passed as a parameter:
... and the JavaScript:
Getting relationships
Getting a related object works that a particular object belongs to is done by calling a getter function, as seen below: