2

I'm trying to accomplish the following without the use of extra custom functions:

  • Store selected items in a new array
  • Only enable the textbox if its respective checkbox has been selected
  • Keep track of updated values in new array

Below is a simplified sample of my code:

<div repeat.for="item of itemSearch.rates">
  <input type="checkbox" ref="item.$index" model.bind="item" checked.bind="selectedRates">
  <input type="text" disabled.bind="item.$index.checked == false" value.bind="item.total_value">
</div>

Currently, with the above, only the checked.bind is working. Selected items are stored in my new selectedRates array.

However my disabled.bind is not being triggered at all.
If I remove checked.bind from the checkbox, disabled.bind works perfectly.

Also the updated values appear to pull through to the selectedRates sometimes after submit and the elements have lost focus.
I'm aware that array observation is not officially supported as mentioned here, just something I noticed worth mentioning.

Questions

  1. Is there a way to get checked.bind to work along with the ref & disabled.bind?
  2. Any idea why I'm seeing this array observable-like behavior sometimes?

Update

I found the "observable values" issue to appear intermittently in firebug when logging the selectedRates.
When displaying it in html, the value changes are always updated, with both @Fabio's use of the BindingEngine implementation as well as my checked.bind. Chrome always displays and logs the correct values.
Still no idea why, or if I should be concerned about this.

Community
  • 1
  • 1
JvR
  • 1,022
  • 1
  • 11
  • 29

2 Answers2

2

See the docs

http://aurelia.io/hub.html#/doc/article/aurelia/binding/latest/binding-checkboxes/1

<input type="checkbox" model.bind="product.id" checked.bind="selectedProductIds">
RouR
  • 6,136
  • 3
  • 34
  • 25
1

That's the simplest solution I could find. There might be easier ways.

html

<template>
  <div repeat.for="item of rates">
    <input type="checkbox"  ref="item.check" model.bind="item" change.delegate="updateSelectedRates(item)">
    <input type="text" disabled.bind="item.check.checked" value.bind="item.value">
  </div>

  <br>
  <template repeat.for="rate of selectedRates">${rate.value}</template>
</template>

js

export class App {

  rates =  [ { value: 1 }, {value: 2} ];
  selectedRates = [];

  updateSelectedRates(rate) {
    if (rate.check.checked) {
      this.selectedRates.push(rate);
    } else {
      let index = this.selectedRates.map(rate => rate.value).indexOf(rate.value);
      if (index !== -1) {
        this.selectedRates.splice(index, 1);
      }
    }
  }

}

Running example https://gist.run/?id=0c2911d065e9725f6f86f45a2422b009

Fabio
  • 11,892
  • 1
  • 25
  • 41
  • Thanks for the feedback @Fabio. Adding that `selected` property to my objects will work, but I'd still prefer not to have the custom functions if possible. Could it perhaps be a bug why `checked.bind` is not working along with the `ref` & `disabled.bind`? – JvR Jan 26 '17 at 07:22
  • As for the observable values, I found the issue to only really appear in firebug when logging the selectedRates. When displaying it in html, the value changes are always updated, with both your use of the `BindingEngine` implementation as well as my `checked.bind`. Chrome always displays and logs the correct values. – JvR Jan 26 '17 at 07:28
  • It's assigning the array's index to that property [as explained here](http://stackoverflow.com/questions/28418345/aurelia-repeat-for-access-index-of-item) and works fine with `disabled.bind="item.$index.checked == false"`. Please note the difference between the following examples [with checked.bind](https://gist.run/?id=0c64a1602738774bb7658cf28db726ee) and [without checked.bind](https://gist.run/?id=670c62155647e16d8be5f8a79772ff34) – JvR Jan 26 '17 at 12:27
  • Indeed... you're right. I think you should open an issue in the oficial repo – Fabio Jan 26 '17 at 12:49
  • @JvR I've found an easier solution. See my updated answer – Fabio Jan 26 '17 at 13:02
  • Thanks @Fabio, I will go with a variation of that for now, I have just changed the function to accept `$event` and then check its target, rather than having another `check` property on my object. [See example here](https://gist.run/?id=cb7a93d18cef07b719d13ce085b86786). – JvR Jan 26 '17 at 13:35
  • Oh yes and it does look like a related bug as [reported here.](https://github.com/aurelia/binding/issues/560) – JvR Jan 26 '17 at 13:35