Custom converter implemented using remote module
To implement a custom converter using remote module:
- Create an Angular project to develop a custom converter using remote module.
- Create a custom converter using remote module.
- Implement the business logic of the custom converter using remote module.
- Add the converter implemented using remote module to the Freedom UI page.
1. Create an Angular project to develop a custom converter using remote module
Develop a custom converter in a dedicated npm
package using an external IDE. This example covers the custom converter development in Microsoft Visual Studio Code.
You can create an Angular project to develop a custom converter in multiple ways, similarly to creating an Angular project to develop a custom UI component using remote module. To do this, follow the instruction in a separate article: Custom UI component implemented using remote module.
2. Create a custom converter using remote module
You can implement synchronous or asynchronous custom converters. The procedure for implementing a custom converter is similar for various types of converters.
-
Create an Angular class in the project. To do this, run the
ng g class some_converter_name.converter
command at the command line terminal of Microsoft Visual Studio Code.some_converter_name.converter
is an arbitrary class name.As a result, Microsoft Visual Studio Code will add the
SomeConverterNameConverter
class files to thesrc/app/
project directory. -
Specify that the
SomeConverterNameConverter
class is a converter.-
Open the
some_converter_name.converter.ts
file. -
Create the
SomeConverterNameConverter
class that implements the interface from the@creatio-devkit/common
library. TheSomeConverterNameConverter
class implements one of the following interfaces:Converter<V, R>
for synchronous convertersConverter<V, Promise<R>>
for asynchronous converters
V
is type of convertible attribute.R
is type of conversion result. -
Flag the
SomeConverterNameConverter
class using theCrtConverter
decorator. -
Import the required functionality from the libraries to the class.
-
Save the file.
some_converter_name.converter.ts file/* Import the required functionality from the libraries. */
import { Converter, CrtConverter } from "@creatio-devkit/common";
/* Add the CrtConverter decorator to the SomeConverterNameConverter interface. */
@CrtConverter({
type: 'usr.SomeConverterNameConverter',
})
/* The class for synchronous converters. */
export class SomeConverterNameConverter implements Converter<string, string> {
}
/* The class for asynchronous converters. */
export class SomeConverterNameConverter implements Converter<string, Promise<string>> {
} -
-
Inject dependencies.
- Open the
some_converter_name.converter.ts
file. - Create the
SOME_TOKEN_NAME
instance of theInjectionToken
class (optional). Use the token to work with Angular DI (dependency injection). You can use a client web service instead of the token. - Flag the
SOME_TOKEN_NAME
token using theCrtInject
decorator. - Import the required functionality from the libraries into the class.
- Save the file.
some_converter_name.converter.ts file/* Import the required functionality from the libraries. */
import { InjectionToken } from "@angular/core";
import { Converter, CrtInject } from "@creatio-devkit/common";
/* The SOME_TOKEN_NAME token that works with Angular DI (dependency injection). */
export const SOME_TOKEN_NAME = new InjectionToken<string>('SOME_TOKEN_NAME');
...
/* The class for synchronous converters. */
export class SomeConverterNameConverter implements Converter<string, string> {
constructor(@CrtInject(SOME_TOKEN_NAME) private _separator: string) { };
}
/* The class for asynchronous converters. */
export class SomeConverterNameConverter implements Converter<string, Promise<string>> {
constructor(@CrtInject(SOME_TOKEN_NAME) private _separator: string) { };
} - Open the
-
Register the
SomeConverterNameConverter
converter as a converter and theSOME_TOKEN_NAME
token.- Open the
app.module.ts
file. - Add the
SomeConverterNameConverter
converter to theconverters
section in theCrtModule
decorator. - Add the configuration object to the
providers
section in theNgModule
decorator.- Set the
provide
property toSOME_TOKEN_NAME
. - Set the
useValue
property to space.
- Set the
- Implement the
resolveDependency()
method in thebootstrapCrtModule()
method of theAppModule
root module. ThebootstrapCrtModule()
method registers theSomeConverterNameConverter
converter flagged using theCrtModule
decorator. TheresolveDependency()
method receives the dependencies of theSomeConverterNameConverter
converter. - Import the required functionality from the libraries into the class.
- Save the file.
app.module.ts file/* Import the required functionality from the libraries. */
import { DoBootstrap, Injector, NgModule, ProviderToken } from '@angular/core';
import { bootstrapCrtModule, CrtModule } from '@creatio-devkit/common';
import { SomeConverterNameConverter, SOME_TOKEN_NAME } from './some_converter_name.converter';
@CrtModule({
...,
/* Specify that SomeConverterNameConverter is a converter. */
converters: [SomeConverterNameConverter]
})
@NgModule({
...,
providers: [
{
provide: SOME_TOKEN_NAME,
useValue: ' '
},
],
})
export class AppModule implements DoBootstrap {
constructor(private _injector: Injector) {}
ngDoBootstrap(): void {
/* Bootstrap CrtModule definitions. */
bootstrapCrtModule('some_package_name', AppModule, {
resolveDependency: (token) => this._injector.get(<ProviderToken<unknown>>token),
});
}
} - Open the
-
Build the project. To do this, run the
npm run build
command at the command line terminal of Microsoft Visual Studio Code.
As a result, Microsoft Visual Studio Code will add the build to the dist
directory of the Angular project. The build will have the <%projectName%>
name specified on step 1.
3. Implement the business logic of the custom converter using remote module
-
Implement the conversion of an incoming value.
-
Open the
some_converter_name.converter.ts
file. -
Implement a method of the
SomeConverterNameConverter
interface. Implement one of the following methods:convert(value: V, context: ViewModelContext, ...params: unknown[]): R;
for synchronous convertersasync convert(value: V, context: ViewModelContext, ...params: unknown[]): R;
for asynchronous converters
V
is the type of convertible attribute.R
is the type of conversion result. -
Import the required functionality from the libraries into the class.
-
Save the file.
some_converter_name.converter.ts file/* Import the required functionality from the libraries. */
import { Converter, ViewModelContext } from "@creatio-devkit/common";
...
/* The class for synchronous converters. */
export class SomeConverterNameConverter implements Converter<string, string> {
/* Implement the convert() method for synchronous converters. */
public convert(value: string, context: ViewModelContext, ...params: string[]): string {
return [value, ...params].join(this._separator);
}
}
/* The class for asynchronous converters. */
export class SomeConverterNameConverter implements Converter<string, Promise<string>> {
/* Implement the convert() method for asynchronous converters. */
public async convert(value: string, context: ViewModelContext, ...params: string[]): Promise<string> {
/* Implement some async action. */
...;
await new Promise(function(resolve) {
setTimeout(resolve, 100);
});
return [value, ...params].join(this._separator);
}
} -
-
Build the project. To do this, run the
npm run build
command at the command line terminal of Microsoft Visual Studio Code.
As a result, Microsoft Visual Studio Code will add the build to the dist
directory of the Angular project. The build will have the <%projectName%>
name specified on step 1.
4. Add the converter implemented using remote module to the Freedom UI page
-
Repeat steps 1-6 of the procedure to implement a custom UI component using remote module.
-
Bind the custom converter implemented using a remote module to the
caption
property of the corresponding model attribute in theviewConfigDiff
schema section. Creatio lets you set converter parameters. Learn more in a separate article: Customize page fields.viewConfigDiff schema sectionviewConfigDiff: /**SCHEMA_VIEW_CONFIG_DIFF*/[
...,
{
"operation": "insert",
"name": "SomeComponentName",
"values": {
...,
/* Apply converter to the $SomeAttributeName attribute. Use a synchronous or asynchronous converter. */
"caption": "$SomeAttributeName | usr.SomeConverterNameConverter : 'SomeConverterParameter'",
...
},
...
}
]/**SCHEMA_VIEW_CONFIG_DIFF*/, -
Click Save on the Client Module Designer’s toolbar.
As a result, Creatio will apply the converter to the element on the Freedom UI page.