Skip to content
This repository has been archived by the owner on Apr 12, 2024. It is now read-only.

feat(ngOptions): add $value variable for easier use of trackBy+selectAs #15584

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

icholy
Copy link
Contributor

@icholy icholy commented Jan 5, 2017

What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)

Feature.

What is the current behavior? (You can also link to an open issue here)

Currently it's a pain to use trackBy and selectAs together.

What is the new behavior (if this is a feature change)?

It introduces a $value variable which simplifies how the trackBy expression can be written.

Does this PR introduce a breaking change?

No.

Please check if the PR fulfills these requirements

@Narretz
Copy link
Contributor

Narretz commented Jan 7, 2017

So this is basically a shortcut for (val.id || val.subItem.id)? But in which cases exactly do you need this?
From your changes to the docs, that's not entirely clear, so I would like a better description of this feature.
It's also not enough to change the existing tests, there should be new tests that make explicit what this feature does.

@icholy
Copy link
Contributor Author

icholy commented Jan 7, 2017

So this is basically a shortcut for (val.id || val.subItem.id)?

In the simple case, yes.

But in which cases exactly do you need this?

Here's a contrived example (note the id's don't match up).

$scope.items = [{
  id: 0,
  label: 'aLabel',
  subItem: { name: 'aSubItem', id: 1 }
}, {
  id: 1,
  label: 'bLabel',
  subItem: { name: 'bSubItem', id: 0 }
}];

$scope.selected = $scope.items[0].subItem;

If you use (val.id || val.subItem.id) here, the wrong object will be initially selected.

demo: https://plnkr.co/edit/8MQXJpRqEdtwF1hcCM3w?p=preview

edit: In my actual use case the track by expression was (item.valueOf() || item.subItem.valueOf()) and that wont work because all objects have that method.

@icholy
Copy link
Contributor Author

icholy commented Jan 7, 2017

@Narretz I noticed you put this in the Ice Box milestone and I don't want to invest more time into this if it's not going anywhere. I think that I presented a valid use case in my last comment, but if that's not the case, let me know so I don't waste my time.

@icholy
Copy link
Contributor Author

icholy commented Jan 26, 2017

@Narretz I've add a bit more to the documentation, but I'm not sure about the tests. Here's what it says above the tests I changed.

  /**
   * This behavior is broken and should probably be cleaned up later as track by and select as
   * aren't compatible.
   */

This is what the $value variable aims to fix. So it doesn't make sense to still have these tests using the hacky workaround. If I was going to add tests, they would be identical except for how the track by expression is written.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants