Help ValueConverter


#1

Hi all,

i would like to know why when I use a ValueConverter with 2 parameters and the second parameter is a list it isn’t refreshed after, for example, a click on the element.

Here some screens:

HTML:

TS ONCLICK OF THE ELEMENT:

ARRAY SOURCE:
image

CONVERTER:

Hope that someone could help me.
Best regards,
Andrea


#2

Collection (array, set, map) aren’t observed automatically, for perf reason. You can give Aurelia a little more hint in the scenario described:

<div class="list-group">
  <a repeat.for="serivce of _serivcesSelected">
    ${service | serviceSetup :_servicesSelected :_servicesSelected.length}
    ...
  </a>
</div>

By adding a third 2nd param to your value converter (which will be the 3rd param in toView method), you are hinting Aurelia to observe the length of the array for the binding as well as the array at the property _servicesSelected

Can you try that?


#3

We should really make this the default behavior. It’s the default behavior with a repeat.for, for example.


#4

There are a few reasons why this isn’t default (and why it’s difficult to make it the default in vCurrent)

  • There is no separate AST representation for them (not in vCurrent at least - there is in vNext), so they can’t be connected in the “regular” way.

  • Change tracking is fundamentally different from other kinds of observation (callback returns splices instead of the new value, and also does not return the old value).

  • Change tracking has a huge overhead compared to other forms of observation because of the Levenshtein distance calculation.

So like @bigopon mentioned they are not observed by default for performance reasons. @davismj making it a default would be a huge performance regression in many apps.

Array bindings in a template are usually repeater or select related and those already take care of array observation internally.
When you’re binding the array to some other custom element, you typically instantiate an array observer via the binding engine in the view model.

For a value converter and binding behavior arguments it kind of makes sense to have array observation on by default, just like select and repeat does, but it’s not quite the same thing. For select and repeat the array observation is key to their function and they fully utilize the splices (at least the repeater does).
For converters and behaviors it would be nothing more than an update trigger - the splices would never be used.

In that regard, a clean(er) solution than turning this on by default might be to have an opt-out for the Levenshtein distance / splices calculation. That’d be tricky though. I’m certainly going to give this a try in vNext (where we don’t have this distance calc) and perhaps the rest of the integration can somehow be backported if I figure out a clean opt-out. Not sure yet.