Hello everybody,
I’m trying to do something (which is probably an anti-pattern),
but I need an event published from a values converter.
I have
TypeError: can't access property "publish", this.eventAggregator is undefined
which is most definitely a circular dependency. My question is:
how can I ‘signal’ from a value converter, which is in massive use in the App without have such issues.
I had a previous version where I was trying to do this via service, but again I had these undefined, properties, caused from a Circular dependency.
My code:
import { inject } from 'aurelia-framework';
import { EventAggregator } from 'aurelia-event-aggregator';
import { thousandsSeparator } from '../utils/thousands-separator';
import { CurrencyService } from '../services/currency-service';
import { currencyModes } from '../enums/currency-modes';
import { WrongCurrencyEvent } from '../services/event/wrong-currency-event';
@inject(EventAggregator, CurrencyService)
export class SumFormatValueConverter {
constructor(eventAggregator, currencyService = new CurrencyService()) {
this.currencyService = currencyService;
this.eventAggregator = eventAggregator;
}
toView(value, ...args) {
if (value === null || value === undefined) return;
this.defaultCurrency = this.currencyService.getDefaultCurrency(); // EUR
this.currency = this.currencyService.getCurrency(); // BNG
this.config = Number(this.currencyService.getConfig()); // 0 EUR (BGN), 1 - EUR, -1 - BGN
const { euro, lv } = currencyModes;
const formattedValue = this._normalizeValue(value);
if (!this._isValidFormattedValue(formattedValue)) {
return;
}
const primaryCurrency = this._formatAmount(formattedValue, 'amount');
const secondaryCurrency = this._formatAmount(formattedValue, 'amountSecondary');
_normalizeValue(value) {
const amountSecondary = value?.amountSecondary ?? 0;
const currency = this.defaultCurrency;
switch (true) {
case typeof value === 'number':
case Number.isFinite():
this.eventAggregator.publish(new WrongCurrencyEvent(`Wrong currency value - ${value}`));
return { amount: value, currency, amountSecondary };
case typeof value === 'string' && Number.isFinite(Number(value)):
this.currencyLogger.logCallStack(`Wrong currency value - ${value}`);
return { amount: Number(value), currency, amountSecondary };
case (value?.amount !== 0 && value.amountSecondary === 0) || (value?.amount === 0 && value.amountSecondary !== 0):
this.eventAggregator.publish(new WrongCurrencyEvent(`Wrong currency value(s) - amount: ${value?.amount}, amountSecondary: ${value?.amountSecondary}`));
return { amount: value?.amount, currency, amountSecondary }
default:
return { amount: value?.amount, currency, amountSecondary };
}
}
_isValidFormattedValue(formattedValue) {
return (
...
);
}
_formatAmount(formattedValue, amount) {
// code here
}
_formatOutput(amount, currency) {
return ...
}