# Making a getter @bindable

**URL:** <https://discourse.aurelia.io/t/making-a-getter-bindable/5161>\
**Category:** Help Requests\
**Created:** [May 8, 2023, 2:16pm UTC](https://discourse.aurelia.io/t/making-a-getter-bindable/5161 "2023-05-08T14:16:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Job](https://avatars.discourse-cdn.com/v4/letter/j/6a8cbe/32.png) [@Job](https://discourse.aurelia.io/u/Job)\
**Post date:** [May 8, 2023, 2:16pm UTC](https://discourse.aurelia.io/t/making-a-getter-bindable/5161/1 "2023-05-08T14:16:59Z")

</div>

This is a simplified example.

Let’s say we have a name input component that has input boxes for the `firstName` and `lastName`. But we don’t care about these separate variables, only the combined `fullName`. We want to be able to get that value from the custom component.

`<name-input full-name.from-view="customerName">`

Intuitively, you use a computedFrom getter for this:

```auto
@customElement("name-input")
class NameInput{
  private firstName: string;
  private lastName: string;
  
  @computedFrom("firstName", "lastName")
  public get fullName() { return this.firstName + " " + this.lastName; }
}

```

This is not yet bindable to the outside, so we add a bindable, that only allows data to flow from the component to the outside, not the other way:

```auto
class NameInput{
  private firstName: string;
  private lastName: string;
  
  @bindable({defaultBindingMode: "fromView"})
  @computedFrom("firstName", "lastName")
  public get fullName() { return this.firstName + " " + this.lastName; }
}

```

This does not work…

Also not having only bindable there, or only computedFrom. A getter cannot be bound in Aurelia it seems.

# Current solution

Turn it into a normal variable.  
To mimic the `computedFrom` we need to turn firstName and lastName into observables and recalculate fullName manually.

```auto
class NameInput{
  @observable
  private firstName: string;
  @observable
  private lastName: string;
  
  @bindable({defaultBindingMode: "fromView"})
  public fullName: string;
  
  private setfullName(): void
  {
    this.fullName = this.firstName + " " + this.lastName;
  }
  
  private firstNameChanged(): void
  {
    setFullName();
  }
  private lastNamechanged(): void
  {
    setFullName();
  }
}

```

This solution is far from ideal, having 3 functions and a variable, when 1 getter would do. If the computedFrom has more variables, this will grow large very quickly.

Are there any better solutions?

---

<div class="post-metadata">

**Author:** ![Andy](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/andy/32/1880_2.png) [@Andy](https://discourse.aurelia.io/u/Andy)\
**Post date:** [May 8, 2023, 2:56pm UTC](https://discourse.aurelia.io/t/making-a-getter-bindable/5161/2 "2023-05-08T14:56:51Z")

</div>

Hello, this is not a solution but I understand that in Aurelia 2, the framework automatically computes observation without the need for any configuration!

---

<div class="post-metadata">

**Author:** ![Sayan751](https://yyz1.discourse-cdn.com/flex027/user_avatar/discourse.aurelia.io/sayan751/32/1861_2.png) [@Sayan751](https://discourse.aurelia.io/u/Sayan751)\
**Post date:** [May 8, 2023, 4:47pm UTC](https://discourse.aurelia.io/t/making-a-getter-bindable/5161/3 "2023-05-08T16:47:54Z")

</div>

You can avoid duplication by using the same change handler for all the observables.

```auto
@observable({ changeHandler: 'setfullName' })
private firstName: string;

@observable({ changeHandler: 'setfullName' })
private lastName: string;

```
