javascriptaureliacustom-attribute

How to signal a custom attribute in Aurelia?


For a binding, I can use signal in case the value is externally updated. How about a custom attribute?

I have the following data-language custom attribute:

import { customAttribute, autoinject } from 'aurelia-framework';
import { LanguageService } from "./language";

@autoinject
@customAttribute("data-language")
export class LanguageCustomAttribute {

    private value: string;

    constructor(
        private element: Element,
        private languageService: LanguageService) { }

    public bind() {
        var e = $(this.element);
        e.html(this.languageService.getText(e.attr("data-language")));
    }

}

This custom attribute is globally registered using

  aurelia.use
      .standardConfiguration()
      .globalResources("./components/language/data-language");

Now assume user can change a language at runtime (and the result of this.languageService.getText would change). How can I send a signal so all [data-language] elements are updated?


Solution

  • This is pretty much what the Aurelia I18N plugin does as well using the EventAggregator to update the custom attributes value on the fly when noticed. See this Aurelia Discourse discussion for detailed explanation