I am using Aurelia and I have an array of items bound to a grid and they have a selected property on them. I want to bind a button to be enabled when any one of the items is true. I can do a brute force approach where I have a getter that is filtering the list and returning the selected items, but that means that I would be doing dirty checking constantly in the app and I don't want to do that. I am hoping for a more efficient approach. Any ideas?
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
I think you can also leverage
EventAggregator
as shown here. In that way there is no need to perform dirty checking all the time and instead handle the item selection event in its own VM and publish the eventdata; the subscriber on the other side will listen to the same and perform the gymnastic needed.However, I have never used it, so I am not sure about the deeper details of it. But from the documentation it looks pretty easy.
Few things you could do- assuming I have your use case right:
dirty-checking (it's only one property- not a big deal)
observe the items
use a collection class
use a
selectedItems
array instead of a selected boolean propfor binding purposes, use
selectedItems.length
as your "any selected" propertyJeremy got me thinking about this in this bug. So it looks like you can also get the binding refreshing via a custom Binding Behaviors. Hopefully Jeremy can confirm I'm not doing anything too silly here.
Used like this:
repeat.for="item of items | filter & array:'propertyName'"
It overrides the standard observe behaviour and observes on the array and the property you define on each item. It can probably be improved to be more generic...
In addition to the Jeremy's examples, you can create a custom setter, for example:
I've created a plunker for you: http://plnkr.co/edit/OTb2RDLZHf5Fy1bVdCB1?p=preview
Doing that, you'll never be looping to see if some item has changed.