Compose Argument References

This tutorial shows you how to reuse an argument in multiple statements in a compose method. This reuse is useful when you want to access an argument passed to a compose method in multiple statements.

We declare a reusable passwordStr argument for the signup method at the same level as the name property.

    "methods": [
        {
            "name": "signup",
            "args": [
                {
                    "name": "passwordStr",
                    "type": "string"
                }
            ],
            "compose": [ ... ]
        }
    ]

Declare the argument reference in each compose statement where you want to access the reusable argument by setting the argument's type to argumentReference.

   "compose": [
       ...,
       {
           "element": "password",
           "apply": "clearAndType",
           "args": [
               {
                   "name": "passwordStr",
                   "type": "argumentReference"
               }
           ]
       },
       {
           "element": "passwordConfirmation",
           "apply": "clearAndType",
           "args": [
               {
                   "name": "passwordStr",
                   "type": "argumentReference"
               }
           ]
       },
       ...
   ]

The statements that apply actions to the password and passwordConfirmation elements both declare a reference to the reusable passwordStr argument.

The Compiled Types tab in the upper-right panel shows the signature for signup with two arguments.

signup(username: string, passwordStr: string): Promise<void>;

The Compiled JS tab in the upper-right panel shows the generated implementation for signup where the passwordStr argument is passed to the two actions.

    async signup(username, passwordStr) {
        ...

        const _statement1 = await this.getPassword();
        await _statement1.clearAndType(passwordStr);
        const _statement2 = await this.getPasswordConfirmation();
        await _statement2.clearAndType(passwordStr);

        ...
    }

The test calls signup and passes two arguments.

await signupFormRoot.signup('[email protected]', '$ecur3Pass');

The first argument is the username, which is the first argument declared in the compose method. The second argument is the passwordStr, which is referenced in the statements that apply actions to the password and passwordConfirmation elements. The passwordStr argument is used in both these statements.

Finally, the test asserts that the username, password and passwordConfirmation element values match the argument values passed to signup.