What is Settings
Module a-settings
provides the Settings
feature, which can display the Settings
management page of each module in the front end, so as to facilitate the unified configuration
Because the
Settings
uses theform validation
mechanism, you only need to provide theJSON Schema
related to theSettings
Settings Types
Each module can provide two Settings
:
User Settings
:user related settingsInstance Settings
:instance related settings
Definition of Schema and Validation
src/suite-vendor/test-party/modules/test-party/backend/src/meta.js
(Be omitted)
Definition of Settings
Specify validator
for user settings
and instance settings
respectively in meta
of the module
src/suite-vendor/test-party/modules/test-party/backend/src/meta.js
settings: {
user: {
validator: 'userTest',
},
instance: {
validator: 'instanceTest',
},
},
Name | Description |
---|---|
settings.user | user settings |
settings.user.validator | validator for user settings |
settings.instance | instance settings |
settings.instance.validator | validator for instance settings |
Default Values
Specify the default values for user settings
and instance settings
respectively in config
of the module
src/suite-vendor/test-party/modules/test-party/backend/src/config/config.js
// settings
config.settings = {
instance: {
groupInfo: {
slogan: '',
},
},
user: {
groupInfo: {
username: 'zhennann',
},
groupExtra: {
panelExtra: {
groupInfo: {
mobile: '123',
sex: 1,
language: 'en-us',
},
},
},
},
};
Usage of Settings
-
Page at Frontend: In the frontend, you can directly manage the
settings
of each module through theSettings
menu -
API at Backend: In the backend, module
a-settings
injects objectsettings
intoctx.meta
through middleware mechanism`
src/suite-vendor/test-party/modules/test-party/backend/src/controller/test/feat/settings.js
Api: User Settings
getUser
Get the value of a field
let value = await this.ctx.meta.settings.getUser({
name: '/groupInfo/username'
});
Name | Description |
---|---|
name | field name in JSON Pointer format |
loadSettingsUser
Get the complete value of settings
let data = await this.ctx.meta.settings.loadSettingsUser();
let value = data.groupInfo.username;
saveSettingsUser
Save the complete value of settings
data.groupInfo.username = 'zhennann';
await this.ctx.meta.settings.saveSettingsUser({ data });
Api: Instance Settings
getInstance
Get the value of a field
let value = await this.ctx.meta.settings.getInstance({
name: '/groupInfo/slogan'
});
Name | Description |
---|---|
name | field name in JSON Pointer format |
loadSettingsInstance
Get the complete value of settings
let data = await this.ctx.meta.settings.loadSettingsInstance();
let value = data.groupInfo.slogan;
saveSettingsInstance
Save the complete value of settings
data.groupInfo.slogan = 'Less is more, while more is less';
await this.ctx.meta.settings.saveSettingsInstance({ data });
Comments: