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

Each collection.getAllSorted uses the same result object, needs to be a copy #352

Closed
zachleat opened this issue Dec 20, 2018 · 1 comment
Assignees
Labels

Comments

@zachleat
Copy link
Member

https://www.11ty.io/docs/collections/#getallsorted() and maybe other methods too, needs to be looked at.

Most of the time this might be okay (maybe? No one has noticed this but me 😅) but this is super intuitive when you use Array methods that perform operations in place, like .reverse(), it can give you unreliable results when you use it in two different calls.

Notably: the following will return two different results even though the code is the same 😭

eleventyConfig.addCollection("myPostsReverse", function(collection) {
  return collection.getAllSorted().reverse();
});
eleventyConfig.addCollection("myOtherPostsReverse", function(collection) {
  return collection.getAllSorted().reverse();
});

Workaround for now is to create a copy of your results like so:

eleventyConfig.addCollection("myPostsReverse", function(collection) {
  return collection.getAllSorted().slice(0).reverse();
});

The obvious fix here is to do this by default in the getAllSorted method.

@zachleat
Copy link
Member Author

zachleat commented Jan 7, 2019

@zachleat zachleat closed this as completed Jan 7, 2019
@zachleat zachleat self-assigned this Jan 7, 2019
zachleat added a commit that referenced this issue Jan 7, 2019
@zachleat zachleat removed this from the 0.7.0 milestone Jan 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant