.bind vs string interpolation in aurelia
Asked Answered
H

2

6

In our code base we have a mixture of the following:

  1. attribute="${something}", attribute="${something | converter}", etc.
  2. attribute.bind="something", attribute.bind="something | converter"

I find the latter easier to read.

The examples I'm referring to are exactly like the above; i.e., they do not add any additional string content.

I think that it's easier on Aurelia too. Am I correct?

Also, for these specific cases where no actual interpolation is involved, is there any benefit to the first form? (other than it is two characters less to type.)

Hakan answered 14/2, 2017 at 14:11 Comment(0)
S
4

Given the examples you have shown, I would recommend using option 2. It really isn't "easier on Aurelia," but it is more explicit that you are binding the value of that attribute to the property listed.

Original Answer Below

The benefit of the first option is when you have, for example, an attribute that accepts many values but as a single string. The most common example of this is the class attribute. The class attribute accepts multiple classes in a space-separated list:

<div class="foo bar baz"></div>

Imagine we only want to add or remove the class baz from this list based on a prop on our VM someProp while leaving the other classes. To do this using the .bind syntax, we would have to create a property on our VM that has the full list but adds or removes baz as determined by the value of someProp. But using the string interpolated binding, this becomes much simpler:

 <div class="foo bar ${someProp ? 'baz' : ''}"></div>

You can imagine how this could be extended with multiple classes being added or removed. You could maybe create a value converter to do this using the .bind syntax, but it might end up with something that wasn't as readable.

I could imagine a value converter being created that might look something like this in use:

 <div class.bind="someProp | toggleClass:'baz':'foo':bar'"></div>

I really think this is much less readable than using the string interpolation syntax.

By the way, the value converter I imagined above would look like this:

export class ToggleClassValueConverter {
  toView(value, toggledClass, ...otherProps) {
    return `${otherProps.join(' ')} ${value ? toggledClass : ''}`;
  }
}

The best part is that I'm still using string interpolation in the value converter :-)

Statutable answered 14/2, 2017 at 14:40 Comment(4)
'The examples I'm referring to are like the above, they don't add any additional string content.' :) So the cases in question are always of the form ="${...}", they're not things like class="class1 ${...}", which I agree would be a good use case for interpolation.Hakan
Yep, but you asked if there was any benefit to the string interpolation form, so I gave an example of where it is simpler to use it.Statutable
I thought I'd made it clear in the question that I was specifically asking about the cases where strings weren't actually being interpolated. I'll rephrase it to make it more obvious. Cheers.Hakan
I've updated the answer to specifically answer the question as now asked.Statutable
H
0

After wading through the tabs I'd already opened I found this. Although it's not quite the same thing, and it's a bit old, there's a similar thing talked about on https://github.com/aurelia/templating-binding/issues/24#issuecomment-168112829 by Mr Danyow (emphasis mine)

yep, the symbol for binding behaviors is & (as opposed to | for value converters).

<input type="text" data-original="${name & oneTime}" value.bind="name" />

Here's the standard way to write a one-time binding. This will be a bit more light-weight in terms of parsing and binding:

<input type="text" data-original.one-time="name" value.bind="name" />

I don't know if it applies to the .bind/${name} case as well as the oneTime one in the example, but perhaps if it comes to his attention he can say either way.

Given this isn't a cut and dry answer, I'll be marking Ashley's as the answer as it confirms the legibility question and provides useful information on other use cases should anyone else search on similar terms.

Hakan answered 14/2, 2017 at 17:51 Comment(1)
I will defer to Jeremy here. He's definitely the expert.Statutable

© 2022 - 2024 — McMap. All rights reserved.