user

You may often want to associate things with the current enrolled user in your app. For this purpose, JourneyApps provides a global user variable that you can access from any view's JavaScript/TypeScript.

For example, let's say we want to associate objects that a user creates in the mobile app with that particular user. If we have a form object that belongs to the user as shown below in our Data Model:

schema.xml
<?xml version="1.0" encoding="UTF-8"?>
<data-model>
    <model name="user" label="User">
        <field name="name" label="Name" type="text:name" />
        
        <has-many model="form" name="forms" />
        
        <display>{name}</display>
        </model>
        
        <model name="form" label="Form">
        <field name="date_completed" label="Date Completed" type="date" />
        
        <belongs-to model="user" />
        
        <display>{date_completed}</display>
    </model>
</data-model>

...then we can associate a new form (that we create in the init() function in one of our views) with the current user as follows, making use of the global user object:

// This function is called each time the view is loaded
function init() {
    // initialize any data here that should be available when the view is shown
    view.form = DB.form.create();
    view.form.user(user);
}

Last updated