Transient value converter

Is it possible to have a transient value converter? Tried adding @transient decorator but the converter is only instantiated once still.

1 Like

Hi @jbockle,

Instead of using the @transient decorator, you could try to use the registerTransient method of the Container Registration API during your app’s startup. I’m not sure if it will fix your issue, since I have not tried/verified this approach myself and I am not familiar with the way how and when Aurelia internally uses child containers to register “scoped” components for dependency injection.

You seem to have an interesting design scenario. Normally, a value converter only contains some straightforward behavior in the form of a toView method, optionally supplemented with a fromView method for bi-directional support. And thus it normally would not contain any state that would require transient instancing. As I see it, any additional data or “dynamic” behavior of a value converter could also be passed in additional arguments to its toView and fromView methods.

Personally, if I were confronted with a case where additional arguments would not suffice for my value converter, I would seriously consider re-designing a part of my app’s logic, since a value converter probably does not seem to be the #1 choice for implementing such complex logic. A custom element might be a more interesting choice to render complex dynamic data, for example.

Are you really sure you need transient instancing of a value converter in your app?

1 Like

Thanks for your feedback @Bart. Unfortunately registerTransient didn’t work either–I did some digging and the aurelia-binding ValueConverterResource has an instance property that instantiates the value converter once from the container, so it doesn’t appear there is any way to support transient value converters.

However what you are saying makes–I just find it somewhat messy to pass multiple parameters to a value converter, where a consumer of a converter doesn’t really have a way from the html view to discover the API except by reading documentation/code for that value converter. @hiaux0, maybe a future update to the vscode extension could add support for contextual help of a value converter?

1 Like

Try telling aurelia to use a static resolver, similar to https://github.com/aurelia-ui-toolkits/aurelia-mdc-web/blob/master/packages/validation/src/mdc-validation-controller-factory.ts

1 Like

consumer of a converter doesn’t really have a way from the html view to discover the API

Yes def. possible!


Just jotting down approach
[Example from official docs]

For eg.

    <tr repeat.for="repo of repos | sort:column.value:direction.value | take:10">
  1. Save classes TakeValueConverter and SortValueConverter to eg.
autocomplete.valueConvertes
  1. Extract arguments information from toView
 export class SortValueConverter {
    toView(array, propertyName, direction) {

How: AST explorer
“FunctionDeclaration” → “parameters”

  1. In the view trigger intellisense upon “|” with info from 1. and 2.
2 Likes

I wanted to share some progress with the vscode extension and the value converter feature.
(No ETA on release unfortunately)

Current:

  • Completion for value converters

  • Completion of corresponding toView arguments
    au-lsp-completion-1

  • Go to definition

4 Likes

this is really great progress thanks! very excited to try it out

1 Like