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

Updating track by $index usage #16334

Closed
wants to merge 1 commit into from
Closed

Updating track by $index usage #16334

wants to merge 1 commit into from

Conversation

michael-letcher
Copy link

Updating docs to be more clear about the pitfalls of track by $index

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

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

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

Does this PR introduce a breaking change?
No

Please check if the PR fulfills these requirements

  • The commit message follows our guidelines
  • Fix/Feature: Docs have been added/updated
  • Fix/Feature: Tests have been added; existing tests pass

Other information:
REF #16332

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If your company signed a CLA, they designated a Point of Contact who decides which employees are authorized to participate. You may need to contact the Point of Contact for your company and ask to be added to the group of authorized contributors. If you don't know who your Point of Contact is, direct the project maintainer to go/cla#troubleshoot.
  • In order to pass this check, please resolve this problem and have the pull request author add another comment and the bot will run again.

1 similar comment
@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If your company signed a CLA, they designated a Point of Contact who decides which employees are authorized to participate. You may need to contact the Point of Contact for your company and ask to be added to the group of authorized contributors. If you don't know who your Point of Contact is, direct the project maintainer to go/cla#troubleshoot.
  • In order to pass this check, please resolve this problem and have the pull request author add another comment and the bot will run again.

@michael-letcher
Copy link
Author

I signed it!

@googlebot
Copy link

CLAs look good, thanks!

1 similar comment
@googlebot
Copy link

CLAs look good, thanks!

* will not be updated even when the corresponding item changes, essentially causing the view to get
* out-of-sync with the underlying data.
* {@link guide/expression#one-time-binding one-time bindings} or {@link guide/directive#creating-directives directives}.
* In such cases, where the array changes and `track by $index` is in use the `nth` DOM element
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look like it specifies what you mean, coz this works fine: https://plnkr.co/edit/dxzgjmE5MEA0bwQdckVS?p=preview aswell as https://plnkr.co/edit/2i0WKKzh1uDlj4kOrBFN?p=preview

Even tho I'm changing the array.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://plnkr.co/edit/ytQ9p6YJOfrDubCGIc2N?p=preview

Click the remove and you will see the the UI is not updated and that the fail becomes green.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, it just that making a simple change to ur plunkr solves the issue: https://plnkr.co/edit/d39OLmANDGv8EuxXxA6k?p=preview (I replaced splice with slice)

Hence it's not very clear that this is the problem with arrays. (basicly I guess it's immutable vs changing the existing array)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting, what does $filter use? As this is what we are using that causes this issue.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$filter uses Array.prototype.filter, which also doesn't manipulate the array but instead returns a new array. See: https://github.com/angular/angular.js/blob/master/src/ng/filter/filter.js#L172

@michael-letcher
Copy link
Author

Messed up the commit messages, unsure what the process is to fix this. (not going to use the git cli unless specified as the documentation does not say what to do if missed)

<type>Docs: Updating track by $index usage <subject>

<body>
Updating docs to be more clear about the pitfalls of track by $index
<footer>

@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this State. It's up to you to confirm consent of the commit author(s) and merge this pull request when appropriate.

1 similar comment
@googlebot
Copy link

So there's good news and bad news.

👍 The good news is that everyone that needs to sign a CLA (the pull request submitter and all commit authors) have done so. Everything is all good there.

😕 The bad news is that it appears that one or more commits were authored by someone other than the pull request submitter. We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that here in the pull request.

Note to project maintainer: This is a terminal state, meaning the cla/google commit status will not change from this State. It's up to you to confirm consent of the commit author(s) and merge this pull request when appropriate.

@petebacondarwin
Copy link
Member

@michael-letcher I pulled down your branch, squashed the commits and force-pushed back to your branch. This has the effect of updating this PR.

Clarify the pitfalls of using `track by $index`

Closes #16332
@frederikprijck
Copy link
Contributor

frederikprijck commented Nov 20, 2017

Avoid using track by $index when the repeated template contains ...

This reads as if it's fine to use in all other situations.

Despite the fact I agree track by $index can be documented better as it currently is, I'm not sure whether it's something that's supposed to be promoted. Wouldn't it be a better idea to mention it's to be avoided (or adviced not to be used), unless you actually know what you're using it for (and mention some of it's downsides)?

Theoretically, it'll bring in a performance boost. Practically it's not something you want to be using, right ?

Wouldn't it be more beneficial to focus on tracking by something useful instead? To be fair, what's to be said of track by $index that would make u use it in most applications?

@michael-letcher
Copy link
Author

michael-letcher commented Nov 21, 2017

@frederikprijck I agree, should I change the documentation to reflect this?

@gkalpak Also mentioned this:

in practice, tracking by $index is rarely useful

@petebacondarwin
Copy link
Member

@michael-letcher - please do update to reflect this. You will need to pull down the latest commits from your branch before you try to make changes, since I pushed to your branch when I squashed.

git checkut patch-1
git pull

This should update everything locally (and there should be a message about how the branch had been force pushed).

Then when you have made the change be sure to amend the current commit, in the terminal you would use git commit --amend.

@Narretz
Copy link
Contributor

Narretz commented Nov 21, 2017

track by $index is perfectly usable when you have only simple interpolation / expressions in the template, but not one-time bindings, or directives - all content that is static. I agree that we should only mention track by $index at the end of the section, with all the current and extra caveats. At the moment, we lead with it, which is bad.

@frederikprijck
Copy link
Contributor

frederikprijck commented Nov 21, 2017

@Narretz I read this often, yet I added working plunkers with track by $index and updates to the underlying data used which seem to work properly. I'm abit confused why tho...

https://plnkr.co/edit/dxzgjmE5MEA0bwQdckVS?p=preview
https://plnkr.co/edit/2i0WKKzh1uDlj4kOrBFN?p=preview
https://plnkr.co/edit/d39OLmANDGv8EuxXxA6k?p=preview

track by $index is perfectly usable when you have only simple interpolation / expressions in the template

It's usable, but doesn't make alot of sense to track by an index in an array in most cases.

I agree that we should only mention track by $index at the end of the section

Ye, and focus on alternatives, like tracking by a property (even tho that doesn't have to be part of this PR I guess)

@franciscovelez
Copy link
Contributor

@michael-letcher I just changed one line in the docs in the past. If you need my consent for this change, I'll give it to you. Should I need to do anything more?

@Narretz
Copy link
Contributor

Narretz commented Nov 21, 2017

@franciscovelez I think @michael-letcher mistakenly @-ed you. I guess he wanted to @frederikprijck

@michael-letcher
Copy link
Author

michael-letcher commented Nov 21, 2017

@Narretz I did, whoops!

I'll review the documentation again, and perhaps reorder the notion of track by to clearly state what Angular will be doing with it e.g. keep func. that is what gives the performance boost and not mention that using $index gives the benefit.
Also giving some nice examples of this would be good, one tracking by ID with directives and one using index, perhaps?

@franciscovelez
Copy link
Contributor

franciscovelez commented Nov 22, 2017

@franciscovelez I think @michael-letcher mistakenly @-ed you. I guess he wanted to @frederikprijck

🤔 It make sense hahaha

Copy link
Contributor

@Narretz Narretz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, the re-using of elements can also have an impact when using track by 'unique id'.
See https://plnkr.co/edit/24fcSM9h2AzgVvSt9ef4?p=preview

If you click "update" then you can see that for John 2, "fail" changes to "pass", but the directive is not re-compiled (the status color does not change) and the one-time binding is not updated.

track by $index only adds a special case: when you remove item 0, ngRepeat re-uses element 0, but with item 1's scope.
(If you use track by 'unique id', then ngRepeat re-uses element 1)

I wonder if we can make this clearer in the docs?

* will not be updated even when the corresponding item changes, essentially causing the view to get
* out-of-sync with the underlying data.
* {@link guide/expression#one-time-binding one-time bindings} or {@link guide/directive#creating-directives directives}.
* In such cases, where the array changes and `track by $index` is in use the `nth` DOM element
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where ... -> when ..
in use the -> in use, the

* will always be matched with the `nth` item of the array. Since ngRepeat thinks that this item is the same as before,
* it will not re-create the DOM, but keep the existing one (bound to the existing scope), and therefore
* any directives that appear on the template will not be re-compiled (since compilation/linking happens only when a new instance is created).
* This is ngRepeats "keep track" function at play and it to reduce unnecessary DOM elements being rebuilt.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this last sentence adds useful info.

* In such cases, where the array changes and `track by $index` is in use the `nth` DOM element
* will always be matched with the `nth` item of the array. Since ngRepeat thinks that this item is the same as before,
* it will not re-create the DOM, but keep the existing one (bound to the existing scope), and therefore
* any directives that appear on the template will not be re-compiled (since compilation/linking happens only when a new instance is created).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please keep this line at 100 characters

Narretz added a commit to Narretz/angular.js that referenced this pull request Jan 9, 2018
- deduplicate info between docs section and arguments
- don't draw too much attention to track by  ...
- ... but highlight its drawbacks when used with one-time bindings
- add example to show how tracking affects collection updates
- clarify duplicates support for specific tracking expressions

Closes angular#16332
Closes angular#16334
Narretz added a commit that referenced this pull request Jan 17, 2018
- deduplicate info between docs section and arguments
- don't draw too much attention to track by $index  ...
- ... but highlight its drawbacks
- add example to show how tracking affects collection updates
- clarify duplicates support for specific tracking expressions

Closes #16332
Closes #16334 
Closes #16397
Narretz added a commit to Narretz/angular.js that referenced this pull request Jan 17, 2018
- deduplicate info between docs section and arguments
- don't draw too much attention to track by $index  ...
- ... but highlight its drawbacks
- add example to show how tracking affects collection updates
- clarify duplicates support for specific tracking expressions

Closes angular#16332
Closes angular#16334 
Closes angular#16397
Narretz added a commit that referenced this pull request Jan 17, 2018
- deduplicate info between docs section and arguments
- don't draw too much attention to track by $index  ...
- ... but highlight its drawbacks
- add example to show how tracking affects collection updates
- clarify duplicates support for specific tracking expressions

Closes #16332
Closes #16334 
Closes #16397
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants