Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[enhancement] track-by to allow using path instead of key #2632

Closed
dragantl opened this issue Apr 7, 2016 · 7 comments
Closed

[enhancement] track-by to allow using path instead of key #2632

dragantl opened this issue Apr 7, 2016 · 7 comments

Comments

@dragantl
Copy link

dragantl commented Apr 7, 2016

Vue.js version

1.0.20

Reproduction Link

https://jsfiddle.net/ardoramor/hyk86ca3/

Steps to reproduce

Run fiddle, examine console.

What is Expected?

Entries will be tracked by the specified path, just like in filters (e.g., orderBy)

What is actually happening?

VueJs prints out a warning and fails to get the unique value specified by path

Required change

There are three lines that would need to change from value[trackByKey] to getPath(value, trackByKey) in functions cacheFrag, getCachedFrag, and deleteCachedFrag. I can make a PR but maybe such a small change doesn't require it?

Also, is it possible to add expression evaluation for track-by? This way, a combination of fields can yield a unique identifier (e.g., track-by="employee.company + employee.id", in case id can be reused by companies). I have a different application but this should make sense as well.

@Twiknight
Copy link

The feature is cool, but ...err, if track-by works like then-by, I think that would be kind of abuse.

http:https://vuejs.org/guide/list.html#track-by, according to this piece, track-by should be considered as identifier rather than comparer.

If you really need a then-by, refer to this issue: #2591

As for the warning info, I've no idea why it appears.
I can't figure out any duplication in your fiddle.


update:

I got why warning comes out.
track-by can not retrieve data from children objects, it works just like id = value[trackByKey]
so you got two duplicated undefined

@paulpflug
Copy link

I think both of your proposals would be a good enhancement.

@dragantl
Copy link
Author

dragantl commented Apr 7, 2016

@Twiknight Duplication happens because the track-by identifier is used as a key. Here is the excerpt from the code:

id = trackByKey ? trackByKey === '$index' ? index : value[trackByKey] : key || value;

As you can see, id is derived by using value[trackByKey], which results in undefined and results in duplicate. What I propose is the following:

id = trackByKey ? trackByKey === '$index' ? index : getPath(value, trackByKey) : key || value;

So the key or path will be valid with getPath.

I'll look into then-by, as I haven't heard of it before.

@Twiknight
Copy link

@dragantl I think I mistook your proposal, I thought you were trying to make track-by a comparer.
And now I got it.

I totally agree with you.


BTW, I mentioned then-by as a generic feature, not referring to a certain directive

@yyx990803
Copy link
Member

This makes sense, I'd welcome a PR if anyone wants to take a crack at it.

@dragantl
Copy link
Author

dragantl commented Apr 7, 2016

I'll make a PR

@yyx990803
Copy link
Member

@dragantl oops, I was just working on it. Thanks for the offer tho!

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

No branches or pull requests

4 participants