tachybase_todo/packages/core/client/src/variables
Zeke Zhang c6915c69f8
fix: use appends param to load association data (#3282)
* fix: use appends param to load association data

* chore: update yarn.lock

* test: add test

* test: remove the 'BUG:' text

* test: fix 'window is not defined'

* test: increase timeout
2023-12-29 11:34:27 +08:00
..
__tests__ refactor: establish a sound testing system (#3179) 2023-12-21 20:39:11 +08:00
hooks feat(variable): add current role (#3167) 2023-12-08 19:19:53 +08:00
utils fix: fix display association fields with subform (#3036) 2023-11-14 14:15:47 +08:00
constants.ts fix(subtable): should not have a value by default and fix key of table (#2763) 2023-10-08 20:55:30 +08:00
index.ts feat: kanban& gantt&bulk edit& bulk update& duplicate& print action pluggable (#3019) 2023-12-16 21:59:33 +08:00
README.md refactor: new schema initializer and schema settings (#2802) 2023-12-04 14:56:46 +08:00
README.zh-CN.md refactor: new schema initializer and schema settings (#2802) 2023-12-04 14:56:46 +08:00
types.ts fix: use appends param to load association data (#3282) 2023-12-29 11:34:27 +08:00
VariablesProvider.tsx fix: use appends param to load association data (#3282) 2023-12-29 11:34:27 +08:00

group
title order
Client 1

useVariables

Usage

const { registerVariable, parseVariable } = useVariables();

// Register variable
registerVariable({
  name: '$user',
  // If you are certain that the `association field` data does not need to be loaded on-demand in the variable, you can omit this field
  collectionName: 'users',
  ctx: {
    name: 'Zhang San',
    nickname: 'Xiao Zhang',
  },
});

// Parse variable
const userName = await parseVariable('{{ $user.name }}');
console.log(userName); // 'Zhang San'

Built-in Global Variables

Some variables that are used globally are registered within the VariablesProvider. These variables are defined in useBuiltinVariables and can be modified by changing the return value of useBuiltinVariables.

Local Variables

When using the parseVariable method in useVariables, in addition to parsing based on the built-in global variables, you can also use some temporary local variables.

const { parseVariable } = useVariables();
const localVariable = {
  name: '$user',
  // If you are certain that the `association field` data does not need to be loaded on-demand in the variable, you can omit this field
  collectionName: 'users',
  ctx: {
    name: 'Zhang San',
    nickname: 'Xiao Zhang',
  },
}

// Use local variable for parsing
const userName = await parseVariable('{{ $user.name }}', localVariable);
console.log(userName); // 'Zhang San'

The registered local variables will be automatically destroyed after parsing and will not affect the value of global variables.

useLocalVariables

This hook encapsulates some variables that are quite common but cannot be treated as global variables.